1
0
mirror of https://github.com/jbranchaud/til synced 2026-07-03 08:08:24 +00:00

Add Use Rescue As Part Of Inline Statement as a Ruby TIL

This commit is contained in:
jbranchaud
2026-06-26 11:54:31 -05:00
parent c8f8c2c1a3
commit b91527db30
2 changed files with 65 additions and 1 deletions
+2 -1
View File
@@ -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). 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: 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) - [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 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 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) - [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) - [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) - [What To Do When You Don't Rescue](ruby/what-to-do-when-you-dont-rescue.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 '<main>': 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.