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
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.
···
On Thu, Mar 12, 2009 at 11:10 AM, Jason Lillywhite <jason.lillywhite@gmail.com> 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}
I'm being silly (its fun being silly with inject :).
This is much easier:
h.each {|k, v| h[k] = v * 2 }
lasitha.
···
On Thu, Mar 12, 2009 at 11:37 AM, lasitha <lasitha.ranatunga@gmail.com> wrote:
On Thu, Mar 12, 2009 at 11:10 AM, Jason Lillywhite > <jason.lillywhite@gmail.com> 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}