From: Thomas Wieczorek Date: 2008-01-29T22:30:55+09:00 Subject: Re: Proposal/RFQ: Hash#values/keys with block On Jan 29, 2008 2:10 PM, Dirk Traulsen wrote: > hash = { 'a' => 1, 'bb' => 2, 'c' => 33, 'dd' => 44} > > hash.keys #=> ["a", "bb", "c", "dd"] > hash.values #=> [1, 2, 33, 44] > > hash.values {|k,v| k > 'a' } #=> [2, 33, 44] > I am sure you could use external iterators for that and extend your Hash class. (I'll try it later) > hash.values {|k,v| v % 2 == 0 } #=> [2, 44] > This one goes easily with a select call: hash.values.select {|el| el % 2 == 0 } #=> [2, 44] > hash.keys {|k,v| k.size == 1 } #=> ["a", "c"] > Same here: hash.keys.select {|el| el.size == 1 } > Alternatively they could be implemented as new methods called for > example > hash.values_if {} > hash.keys_if {} > analogue to the already defined Hash#values_at. > > Well, I would prefer the extension of Hash#keys/values with blocks. > What do you think? > I think Array#select does a fine job. You can still write your own helper methods to implement the behaviour which you need.