mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add If You Detect None as a ruby til
This commit is contained in:
33
ruby/if-you-detect-none.md
Normal file
33
ruby/if-you-detect-none.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# If You Detect None
|
||||
|
||||
The
|
||||
[`Enumerable#detect`](http://ruby-doc.org/core-2.2.3/Enumerable.html#method-i-detect)
|
||||
method, which is synonymous with `#find`, can be given an optional argument,
|
||||
`ifnone`, that is called when nothing in the array meets the conditional in
|
||||
the block. Though I am not sure how this is practically useful and cannot
|
||||
find an example of it in use, this contrived example illustrates how it
|
||||
works.
|
||||
|
||||
```ruby
|
||||
# without the fallback behavior
|
||||
> [2,4,6,8].detect { |x| x.odd? }
|
||||
=> nil
|
||||
|
||||
# with a proc as an argument
|
||||
> [2,4,6,8].detect(->{0}) { |x| x.odd? }
|
||||
=> 0
|
||||
```
|
||||
|
||||
The last example can also be written as:
|
||||
|
||||
```ruby
|
||||
> [2,4,6,8].detect(->{0}, &:odd?)
|
||||
=> 0
|
||||
```
|
||||
|
||||
And if you want to be really explicit:
|
||||
|
||||
```ruby
|
||||
> [2,4,6,8].detect(ifnone=->{0}, &:odd?)
|
||||
=> 0
|
||||
```
|
||||
Reference in New Issue
Block a user