On Aug 23, 2010, at 6:55 PM, Glenn Little wrote:

Just wondering if there's an idiom for this.  Say I have an array "a",
each element is a hash:

a =[ {"a" => "eh", "b" => "bee"},
      {"c" => "see", "d" => "dee"},
      {"e" => "eee", "f" => "eff"}]

And I want to perform some sort of transformation on all the hash
values, but still end up with an array of the hashes.

Is there a cleaner (not necessarily shorter... I prefer elegantly
readable) way than:

a.each do |row|
 row.each { |k,v| {row[k] => v.upcase }
end

v is a reference to the hash element itself, so you can just do this (I also changed your each to each_value because you only need the value:

a.each do |row|
  row.each_value {|v| v.upcase! }
end

Regards,
Jason

--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to