From: "Einar Magnús Boson" Date: 2008-11-18T10:44:14+09:00 Subject: Re: function to select only certain key/value pairs from hash? >>> >> >> >> That is ridiculously inefficient and weird :) >> >> hash = {:a => 1, :b => 2, :c => 3} >> >> selected = [:a, :b].map{|key|{key, hash[key]}} >> >> p selected >> >> # >> [{:a=>1}, {:b=>2}] >> >> > > and now I see that I was stupd. > heh. nevermind <.< > This is what I meant to do, a lot more efficient to look up the values you're lookin for than looping through all keys for every element. hash = {:a => 1, :b => 2, :c => 3, :d => 4, :str => "test"} selected = [:a, :d, :str].inject({}){|result, key| result[key]= hash[key];result} p selected # >> {:str=>"test", :a=>1, :d=>4} .