Carlo Pecchia

Ruby 1.9: on symbols

(03.06.2010)

With version 1.9 of Ruby language, symbols are treated little differently.

Now, regular expressions are more friendly with symbols:

a = [:apples, :bananas, :cherries]
puts a.grep(/ba/)

The results between 1.8 and 1.9 are quite different:

# 1.8 => []
# 1.9 => bananas

Basically, in regexp symbols are “managed” like strings.

With the new construct ”&”, we can easily obtain a Proc object from a symbol:

puts %w(some words here, and other there).map(&:upcase).join(' ')

# 1.9 => SOME WORDS HERE, AND OTHER THERE

Notice also the new compact usage of map.

Italiano

Sei interessato a conoscere tutte le principali novità di Ruby 1.9?

Vedi lo screeencast "pillola" su ThinkCode.TV

Photo credits.


Comments

(Comments)
blog comments powered by Disqus