mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Fetch Warns About Superseding Block Argument as a Ruby TIL
This commit is contained in:
29
ruby/fetch-warns-about-superseding-block-argument.md
Normal file
29
ruby/fetch-warns-about-superseding-block-argument.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Fetch Warns About Superseding Block Argument
|
||||
|
||||
[Ruby's `#fetch`](https://ruby-doc.org/core-2.5.1/Hash.html#method-i-fetch) can
|
||||
be used in a couple ways beyond just grabbing the value out of a hash.
|
||||
|
||||
If you include a second argument in the `#fetch` call, that will be treated as
|
||||
a default value to fallback to when the first argument key doesn't appear in
|
||||
the hash.
|
||||
|
||||
If you instead specify a block argument, that block will be executed when the
|
||||
key is missing.
|
||||
|
||||
What happens when you specify both a second argument and a block argument?
|
||||
|
||||
```ruby
|
||||
data = { taco: 'bell' }
|
||||
|
||||
data.fetch(:burrito, 'house') do
|
||||
puts 'the block gets executed'
|
||||
'shack'
|
||||
end
|
||||
|
||||
warning: block supersedes default value argument
|
||||
the block gets executed
|
||||
=> 'shack'
|
||||
```
|
||||
|
||||
The block argument wins. The second argument is ignored. And Ruby warns you
|
||||
that, "block supersedes default value argument".
|
||||
Reference in New Issue
Block a user