From b91527db301940f3e7afe0112fa77b59f41a31ff Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 26 Jun 2026 11:54:31 -0500 Subject: [PATCH] Add Use Rescue As Part Of Inline Statement as a Ruby TIL --- README.md | 3 +- .../use-rescue-as-part-of-inline-statement.md | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 ruby/use-rescue-as-part-of-inline-statement.md diff --git a/README.md b/README.md index e37d6cc..95e294f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1803 TILs and counting..._ +_1804 TILs and counting..._ See some of the other learning resources I work on: @@ -1567,6 +1567,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Update The Gemfile Bundled With Version](ruby/update-the-gemfile-bundled-with-version.md) - [Use A Case Statement As A Cond Statement](ruby/use-a-case-statement-as-a-cond-statement.md) - [Use dotenv In A Non-Rails Project](ruby/use-dotenv-in-a-non-rails-project.md) +- [Use Rescue As Part Of Inline Statement](ruby/use-rescue-as-part-of-inline-statement.md) - [Use Tap For Better Test Data Setup](ruby/use-tap-for-better-test-data-setup.md) - [Using BCrypt To Create And Check Hashed Passwords](ruby/using-bcrypt-to-create-and-check-hashed-passwords.md) - [What To Do When You Don't Rescue](ruby/what-to-do-when-you-dont-rescue.md) diff --git a/ruby/use-rescue-as-part-of-inline-statement.md b/ruby/use-rescue-as-part-of-inline-statement.md new file mode 100644 index 0000000..15a0db7 --- /dev/null +++ b/ruby/use-rescue-as-part-of-inline-statement.md @@ -0,0 +1,63 @@ +# Use Rescue As Part Of Inline Statement + +In Ruby I typically think of `rescue` as block syntax that I can use to handle +exceptions. + +```ruby +begin + User.update!(password:) +rescue + puts "There was an issue updating the password" +end +``` + +The `rescue` keyword can also be used as part of an inline statement as a way of +providing a _fallback_ value when the first part of the statement raises. + +For instance, if I'm trying to access some value on an array that happens to be +`nil`, it is going to raise: + +```ruby +> scores.first +(irb):7:in '
': undefined method 'first' for nil (NoMethodError) +``` + +I can instead tack on a `rescue 0` which will give it `0` as a fallback value: + +```ruby +> scores.first rescue 0 +=> 0 +``` + +Of course, there are more idiomatic ways to handle this kind of situation in +Ruby. Maybe something like this: + +```ruby +> Array(scores).first || 0 +=> 0 +``` + +Another way I've seen this inline rescue used is to print out the exception +caused by that line of code, using `$!` (the global variable for the most +recently raised exception). + +```ruby +> scores.first rescue puts $! +undefined method 'first' for nil +=> nil +``` + +That is a one-liner for the following: + +```ruby +begin + scores.first +rescue => e + puts e +end +``` + +The big caveat that goes with this is the same one that goes with any other +blanket `rescue` block. If you are indiscriminately rescuing exceptions without +being intentional about what you are rescuing and why, you could be potentially +burying exceptions that you need to know about.