Good notes posted by svoop
RSS feed
svoop -
October 30, 2009
3 thanks
Map-like Manipulation of Hash Values
Let’s say you want to multiply all values of the following hash by 2:
hash = { :a => 1, :b => 2, :c => 3 }
You can’t use map to do so:
hash.map {|k, v| v*2 } # => [6, 2, 4]
However, with merge you can:
hash.merge(hash) {|k,v| v*2 } => {:c=>6, :a=>2, :b=>4}
(The above is Ruby 1.8, in Ruby 1.9 the order is preserved.)
svoop -
February 10, 2009
3 thanks
Cheat Sheet
I have written a short introduction and a colorful cheat sheet for Perl Compatible Regular Expressions (PCRE) as used by Ruby’s Regexp class:
http://www.bitcetera.com/en/techblog/2008/04/01/regex-in-a-nutshell/