Last Updated: February 25, 2016
·
402
· qw3r

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 :)