From: Robert Dober Date: 2007-01-21T17:37:16+09:00 Subject: Re: Hash pairs at? ------=_Part_177293_27561210.1169368634039 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 1/21/07, Trans wrote: > > Seesm like there should be a mehtod for this: > > h = { :a=>1, :b=>2, :c=>3 } > > h.what_method(:a, :c) #=> { :a=>1, :c=>3 } > ^^^^^^^^^^^ > > Or is there some other simple way we're supposed to do this? > > T. I will try to answer your question, but still it will be more questions :( Before thinking about the name, naming really is an art, I am thinking about the interface (1) So there is *args as above (2) Any object responding to #include? We need two names now, great job Robert ;) (1) #grep, wait a minute hash objects already have grep via Enumerable, no idea how it works for hashes though #filter or filter_keys #intersect might just be the wrong name #delete_unless ugly because delete_if takes a block form only my candidate is #for_keys {a=>1, b=>2, c=>3}.for_keys("hi", :alpha) => {"hi" => 2} # for some values of a, b and c ;) Now to (2) #for_keys_from consistent but long (even ugly) #keys_from #intersect #use_keys #shadow maybe a different approach would be to define a method in Enumerable Enumerable#shadow_hash [*1..2].shadow_hash {a=>2, b=>3, c=>5, d=>7, e=>11} => {1=>2, 2=>3} # see above this allows even for a call without the accolades. This all very vague but I feel that such methods would be very useful. Cheers Robert -- "The best way to predict the future is to invent it." - Alan Kay ------=_Part_177293_27561210.1169368634039--