Thanks all.  Greg, yeah, I had typoed my original example a little,
the => was supposed to be a simple assignment as you mentioned.  (I
also left out a brace... sheesh...)

There seem to be conflicting opinions about whether or not it's good
practice to mutate the hash that I am iterating over.  Intuitively I'd
think mutating values would be safe, but it does give pause in general
and I'd think anyone else looking at it less ruby-aware would at least
hitch at it.  I'm trying to avoid that as much as possible.

Also, the "upcase" I was using was just a stand-in for more complex,
custom mutation.

In the end, since this code is going to need to be maintained by more
than just me (including non-ruby-experts), I think I'll go with
"straigthforward" and avoid the case where you have to know reference
vs. value and so on and just build new hashes on the fly and append
them to a new list.  Seems mundane and not particularly elegant, but
at least it's straightforward and more universally maintainable.

-glenn

On Tue, Aug 24, 2010 at 9:25 AM, Jason King <[email protected]> wrote:
>
> 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
>

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

Reply via email to