Skip to content

Instantly share code, notes, and snippets.

@flazz
Created February 22, 2011 03:21
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save flazz/838163 to your computer and use it in GitHub Desktop.
case with predicates
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