From: Siep Korteling Date: 2008-11-18T08:56:16+09:00 Subject: Re: function to select only certain key/value pairs from has Chris Shea wrote: > On Nov 17, 4:46�pm, Chris Shea wrote: >> >> > Posted viahttp://www.ruby-forum.com/. >> >> Hmm... I thought there was a built in method for that. �Here's one >> way: >> >> hash = {:a => 1, :b => 2, :c => 3} >> Hash[*hash.select {|k,v| [:a,:b].include?(k)}.flatten] �#=> >> {:b=>2, :a=>1} >> >> HTH, >> Chris > > Yeah, or the simpler: hash.reject {|k,v| ![:a,:b].include?(k)} And indeed, it's in facets: require 'facets' p hash.slice(:a,:b) # However, this breaks on hash.slice(:a,:d) # and the hash.reject does not hth, Siep Siep -- Posted via http://www.ruby-forum.com/.