Ruby threequals & Proc
I've just learned a some new things about ruby's threequals. Look:
even = ->(x){ (x % 2) == 0 }
even.call(2) # => true
even.call(3) # => false
even === 2 # => true
even === 3 # => false
Since case
it implements the threequals, we can use this lambda as a predicate.
case num
when even then puts "Even"
else puts "Odd"
end
```
Thx RubyTapas :)
Written by István Demeter
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#