# Can't understand for this each

**URL:** <https://www.ruby-forum.com/t/cant-understand-for-this-each/180970>\
**Category:** Ruby\
**Created:** [January 2, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/cant-understand-for-this-each/180970 "2010-01-02T13:21:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 1:21pm UTC](https://www.ruby-forum.com/t/cant-understand-for-this-each/180970/1 "2010-01-02T13:21:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/cant-understand-for-this-each/180970/2 "2010-01-02T14:06:10Z")

</div>

On Sat, Jan 2, 2010 at 6:20 AM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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.”

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 2:48pm UTC](https://www.ruby-forum.com/t/cant-understand-for-this-each/180970/3 "2010-01-02T14:48:53Z")

</div>

Hi,

Am Samstag, 02. Jan 2010, 21:20:02 +0900 schrieb Ruby N.:

> what’s each{ … } in the find\_all method?
> 
> class Hash  
> def find\_all  
> new\_hash = Hash.new  
> each { |k,v| new\_hash[k] = v if yield(k, v) }  
> new\_hash  
> end  
> end

`Hash#each' is not `Array#each’. It is descibed in  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref\_c\_hash.html#Hash.each](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_hash.html#Hash.each).

It does something like

class Hash  
def each  
keys.each { |k|  
yield k, self[k]  
}  
end  
end

A remarkable aspect is that `Hash#each’ yields two values so that  
everywhere in Enumerable’s descriptions “{ |obj| … }” has to be  
replaced with “{ |k,v| … }” or even “|(k,v)|”. For example, you  
have to call

hash.each\_with\_index { |(k,v),i| … }

Bertram

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 3, 2010, 4:13am UTC](https://www.ruby-forum.com/t/cant-understand-for-this-each/180970/4 "2010-01-03T04:13:05Z")

</div>

On Sun, Jan 3, 2010 at 2:36 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ruby N. wrote:
> 
> > Hello,
> > 
> > Please take a look at below.  
> > what’s each{ … } in the find\_all method?  
> > I can’t understand for it. thanks.
> 
> Would it help if you think of it as “self.each” ?

oops that’s right.  
I know what’s each.  
But I don’t understand for a raw each there…  
yes self.each, got it, thanks!

Jenn.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 2, 2010, 7:36pm UTC](https://www.ruby-forum.com/t/cant-understand-for-this-each/180970/5 "2010-01-02T19:36:27Z")

</div>

Ruby N. wrote:

> Hello,
> 
> Please take a look at below.  
> what’s each{ … } in the find\_all method?  
> I can’t understand for it. thanks.

Would it help if you think of it as “self.each” ?

That is, you are calling the method called “each” on the current object  
instance, which in this case is a Hash.
