From: Josh Cheek Date: 2010-01-02T22:05:24+09:00 Subject: Re: can't understand for this each --000e0cd139a8ff99bd047c2e233f Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jan 2, 2010 at 6:20 AM, Ruby Newbee wrote: > Hello, > > Please take a look at below. > what's each{ .. } in the find_all method? > I can't understand for it. thanks. > > class Hash > def find_all > new_hash = Hash.new > each { |k,v| new_hash[k] = v if yield(k, v) } > new_hash > end > end > > squares = {0=>0, 1=>1, 2=>4, 3=>9} > z=squares.find_all { |key, value| key > 1 } > p z > > An English translation might read "For each key value pair in the hash, add it to the new hash if the block submitted returns true." --000e0cd139a8ff99bd047c2e233f--