From: 7stud -- Date: 2009-04-29T15:36:44+09:00 Subject: Re: Hash#map Fredrik wrote: > Is there any good reason why Hash#map does not give back a Hash (Ruby > 1.8 but same in 1.9 as far as I know)? I often find myself writing > these kind of things: > > newhash = oldhash.inject({}) { |h,(k,v)| h[k] = some_operation(v); h } > > but that doesn't look pretty at all in my opinion. I want to just > write like this: > > newhash = oldhash.map { |k,v| some_operation(v) } > class Hash def hashbackmap result = {} self.each do |key, val| result[key] = yield val end result end end h = { "red" => 10, "blue" => 20, "green" => 30 } p h.hashbackmap {|x| x*2} --output:-- {"green"=>60, "blue"=>40, "red"=>20} I HAVE A PATENT!! My licensing fees are cheap: $10 mth. -- Posted via http://www.ruby-forum.com/.