Skip to content

Instantly share code, notes, and snippets.

Forked from dkubb/gist:1130086
Created Aug 7, 2011
What would you like to do?
case with predicates
require 'rubygems'
require 'backports' # aliases Proc#=== to Proc#call
rs = (0..10000).to_a.sample(30)
rs.each do |r|
case r
when lambda { |n| } then puts "#{r} is zero"
when lambda { |n| (n % 5).zero? } then puts "#{r} is fiven"
when lambda { |n| (n % 4).zero? } then puts "#{r} is fourven"
when lambda { |n| (n % 3).zero? } then puts "#{r} is threeven"
when lambda { |n| n.even? } then puts "#{r} is even"
when lambda { |n| n.odd? } then puts "#{r} is odd"
raise 'unpossible'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment