Skip to content

Instantly share code, notes, and snippets.

@emmanuel
Forked from dkubb/gist:1130086
Created August 8, 2011 21:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save emmanuel/1132758 to your computer and use it in GitHub Desktop.
case with predicates
require 'rubygems'
require 'backports' # aliases Proc#=== to Proc#call
rs = (0..10000).to_a.sample(30)
rs.each do |r|
case
when r.zero? then puts "#{r} is zero"
when (r % 5).zero? then puts "#{r} is fiven"
when (r % 4).zero? then puts "#{r} is fourven"
when (r % 3).zero? then puts "#{r} is threeven"
when r.even? then puts "#{r} is even"
when r.odd? then 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