Created
February 22, 2011 03:21
-
-
Save flazz/838163 to your computer and use it in GitHub Desktop.
case with predicates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
threeven = -> n { n % 3 == 0 } | |
fourven = -> n { n % 4 == 0 } | |
fiven = -> n { n % 5 == 0 } | |
rs = (0..10000).to_a.sample(30) | |
rs.each do |r| | |
case r | |
when -> n { n.zero? } | |
puts "#{r} is zero" | |
when fiven | |
puts "#{r} is fiven" | |
when fourven | |
puts "#{r} is fourven" | |
when threeven | |
puts "#{r} is threeven" | |
when -> n { n.even? } | |
puts "#{r} is even" | |
when -> n { n.odd? } | |
puts "#{r} is odd" | |
else raise "unpossible" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment