Forum: Ruby change (and update) values of hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jason L. (Guest)
on 2009-03-12 07:42
how would I do stuff to each value of a hash and return a modified hash?
h = {"a"=> 3, "b"=> 5}
multiply each value by 2 and return
h = {"a"=> 6, "b"=> 10}

this does what I need but does not modify the hash

h.each {|k,v| h[k]*2}
Jason L. (Guest)
on 2009-03-12 07:52
Is this the best solution:

 h = {"a"=> 3, "b"=> 5}

x = {}

h.each {|k,v| x[k] = v*2}

=> {"a"=>3, "b"=>5}

h.replace(x)

=> {"a"=>6, "b"=>10}


?
lasitha (Guest)
on 2009-03-12 08:10
(Received via mailing list)
On Thu, Mar 12, 2009 at 11:10 AM, Jason L.
<removed_email_address@domain.invalid> wrote:
> how would I do stuff to each value of a hash and return a modified hash?
> h = {"a"=> 3, "b"=> 5}
> multiply each value by 2 and return
> h = {"a"=> 6, "b"=> 10}

One way (among several):
$: irb
01> h = { 'a' => 3, 'b' => 5 }
--> {"a"=>3, "b"=>5}
02> h.inject(h) {|h, (k, v)| h[k] = v * 2; h }
--> {"a"=>6, "b"=>10}

solidarity,
lasitha.
lasitha (Guest)
on 2009-03-12 08:17
(Received via mailing list)
On Thu, Mar 12, 2009 at 11:37 AM, lasitha <removed_email_address@domain.invalid>
wrote:
> --> {"a"=>3, "b"=>5}
> 02> h.inject(h) {|h, (k, v)| h[k] = v * 2; h }
> --> {"a"=>6, "b"=>10}

I'm being silly (its fun being silly with inject :).
This is much easier:
h.each {|k, v| h[k] = v * 2 }

lasitha.
Jason L. (Guest)
on 2009-03-12 08:38
> This is much easier:
> h.each {|k, v| h[k] = v * 2 }

thank you.

I was told it could be bad to change values during iteration. That is
why I proposed doing replace. what do you think?
lasitha (Guest)
on 2009-03-12 09:13
(Received via mailing list)
On Thu, Mar 12, 2009 at 12:06 PM, Jason L.
<removed_email_address@domain.invalid> wrote:
>
>> This is much easier:
>> h.each {|k, v| h[k] = v * 2 }
>
> thank you.
>
> I was told it could be bad to change values during iteration. That is
> why I proposed doing replace. what do you think?

There was a recent thread about this:
http://www.nabble.com/Iterating-a-changing-Hash-un...

I believe assigning values to existing keys is safe.

If in doubt, iterate over the keys instead:
h.keys.each {|k| h[k] = h[v] * 2 }

solidarity,
lasitha.
lasitha (Guest)
on 2009-03-12 09:14
(Received via mailing list)
On Thu, Mar 12, 2009 at 12:42 PM, lasitha <removed_email_address@domain.invalid>
wrote:
> If in doubt, iterate over the keys instead:
> h.keys.each {|k| h[k] = h[v] * 2 }
                          ~~~~
oops.  should be   h[k] = h[k] * 2
lasitha.
Rob B. (Guest)
on 2009-03-12 18:07
(Received via mailing list)
On Mar 12, 2009, at 3:12 AM, lasitha wrote:

> On Thu, Mar 12, 2009 at 12:42 PM, lasitha
> <removed_email_address@domain.invalid> wrote:
>> If in doubt, iterate over the keys instead:
>> h.keys.each {|k| h[k] = h[v] * 2 }
>                          ~~~~
> oops.  should be   h[k] = h[k] * 2
> lasitha.


Or use a little sugar:

h.keys.each {|k| h[k] *= 2 }


Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.