From: Trans Date: 2008-11-18T21:39:52+09:00 Subject: Re: function to select only certain key/value pairs from hash? On Nov 18, 3:31 am, Luc Heinrich wrote: > 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} require 'facets/hash/slice' T.