Hidden features of Ruby

From Ruby 1.9 Proc#=== is an alias to Proc#call, which means Proc objects can be used in case statements like so:

def multiple_of(factor)
  Proc.new{|product| product.modulo(factor).zero?}
end

case number
  when multiple_of(3)
    puts "Multiple of 3"
  when multiple_of(7)
    puts "Multiple of 7"
end

Leave a Comment