From: Nobuyoshi Nakada Date: 2007-07-12T10:54:57+09:00 Subject: Re: Returning part of a hash Hi, At Thu, 12 Jul 2007 05:30:03 +0900, barjunk wrote in [ruby-talk:258922]: > I have hash that has about 20 keys. I'd like to create a new variable > with just three of those keys. Example: > > hash = { "key1" => "value1", > "key2" => "value2", > ... > "key20" => "value20" } > > And a function like: > newhash = hash.slice("key2","key5","key7") Hash#slice doesn't exist but Hash#select in recent 1.9 works similarly. keys = %w"key2 key5 key7" newhash = hash.select {|k,v| keys.include?(k)} It would be easy to define Hash#slice with this. -- Nobu Nakada