3 ms·
Proc#=== allows you to do this: def multiple_of( n ) lambda{ |x| x.modulo( n ).zero? } end case 10 when multiple_of( 3 ): puts "3!" when mul
by farrel 16y ago
Proc#=== allows you to do this:
def multiple_of( n )
lambda{ |x| x.modulo( n ).zero? }
end
case 10
when multiple_of( 3 ):
puts "3!"
when multiple_of( 4 ):
puts "4!"
when multiple_of( 2 ):
puts "2!"
end
Basically you can now run code when checking case equality (===).
- petercooper 16y agoSince we're talking Ruby 1.9 here, that code won't even run :-( .. drop the colons and you're good though! :-) But if we're really talking Ruby 1.9, you could also go with: ->(x){ x % n == 0 }
- rue 16y agoOr, more simply: case $stdin.gets.chomp when lambda {|matchobj| matchobj.count("!") > 1 } puts "Quitcher yelling!" when "hi" puts "Hi yourself" end