Marcel Molina Jr. wrote:
> On Wed, Mar 29, 2006 at 05:40:54PM +0900, Srinivas Jonnalagadda wrote:
>> Thank you for pointing out the chained 'select'!
>>
>> Allow me to apologize for the incorrect problem statement.
>>
>> The issue is actually not with downstream 'select's, but with other
>> calls that assume a hash-like behaviour on the part of the container.
>>
>> Examples include Hash#[] and Hash#each_value.
> 
> It's not pretty but...:
> 
>>> h = { 0 => 0, 1 => 2, 2 => 3, 4 => 4 }
> => {0=>0, 1=>2, 2=>3, 4=>4}
>>> Hash[*h.select { |k,v| k % 2 == 0 }.flatten].each_value {|v|
> p v}
> 0
> 3
> 4
> => {0=>0, 2=>3, 4=>4}

What about splitting that up a little...

  class Hash
    def select_hash &blk
  	Hash[ *select{ |k,v| yield k, v }.flatten ]
    end
  end

  h = { 0 => 0, 1 => 2, 2 => 3, 4 => 4 }
  h.select_hash{ |k,v| k % 2 == 0 }.select_hash{ |k,v| k > 0 }

You can chain all you want, and it isn't so ugly. =)

Zach