From: Luc Heinrich Date: 2008-11-18T17:31:53+09:00 Subject: Re: function to select only certain key/value pairs from hash? On 17 nov. 08, at 23:50, Aryk Grosz wrote: > hash.from_keys(:a,:b) => {:a=> 1, :b=> 2} I tried to resist making this a one-liner and went for the clean and explicit way :) class Hash def from_keys(*keys) selected_values = self.values_at(*keys) selected_key_values = keys.zip(selected_values) Hash[*selected_key_values.flatten] end end hash = {:a => 1, :b => 2, :c => 3} p hash.from_keys(:a, :b) => {:a=>1, :b=>2} -- Luc Heinrich - luc@honk-honk.com