Hidden features of Ruby

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Continuing the "Hidden features of ..." meme, let s share the lesser-known but useful features of Ruby programming language.

Try to limit this discussion with core Ruby, without any Ruby on Rails stuff.

See also:

(Please, just one hidden feature per answer.)

Thank you

Answers

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/63998/hidden-features-of-ruby

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils