From: Trans Date: 2007-01-23T05:39:46+09:00 Subject: Re: Hash pairs at? Joel VanderWerf wrote: > Alex LeDonne wrote: > > Maybe I'm late to the party, but instead of select_pair, I'd name the > > method slice, analagous to Array#slice. > > Looks like the rails crowd has beaten us to it: > > http://dev.rubyonrails.org/changeset/5726 > > It might be in facets, too. > > Seems like a good name for a good method. hmm... in Facets, Hash#slice is just an alias for Hash#values_at. #slice is a reasonably good name. however I suggested #select_pair, not as a name for #pairs_at, but as a hash equivalent of #select, to enrich the langauge with greater polymorphism between hash and asscocivtive arrays. {:a=>1, :b=>2}.select{ |k,v| k == :a } #=> [[:a=>1]] [ [:a, 1], [:b, 2] ].select{ |k,v| k == :a } #=> [[:a=>1]] {:a=>1, :b=>2}.select_pair{ |k,v| k == :a } #=> { :a=>1 } [ [:a, 1], [:b, 2] ].select_pair{ |k,v| k == :a } #=> { :a=>1 } Most of the normal enumerable each-related methods could have hash equivalents like this. (I'm not partial to the _pair suffix, but for lack of a better term...) T.