# How does array.each element work?

**URL:** <https://www.ruby-forum.com/t/how-does-array-each-element-work/168564>\
**Category:** Ruby\
**Created:** [May 22, 2009, 12:49pm UTC](https://www.ruby-forum.com/t/how-does-array-each-element-work/168564 "2009-05-22T12:49:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 22, 2009, 12:49pm UTC](https://www.ruby-forum.com/t/how-does-array-each-element-work/168564/1 "2009-05-22T12:49:30Z")

</div>

Hi All,

Here is my code snippet:-  
my\_array = [1,2,3]  
puts my\_array.each {}

or

my\_array = [1,2,3]  
my\_array.each {|x| puts x}

O/p:-  
1  
2  
3

I want to know how the array.each works. How the compiler interprets and  
prints.

Thanks and regards,  
Neela.

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 22, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/how-does-array-each-element-work/168564/2 "2009-05-22T13:13:50Z")

</div>

On Fri, May 22, 2009 at 12:49 PM, Neela megha shyam Chivukula  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> O/p:-  
> 1  
> 2  
> 3
> 
> I want to know how the array.each works. How the compiler interprets and  
> prints.
> 
> Thanks and regards,  
> Neela.

[http://www.ruby-doc.org/core/classes/SOAP/RPC/SOAPMethodResponse.html#M006286](http://www.ruby-doc.org/core/classes/SOAP/RPC/SOAPMethodResponse.html#M006286)  
each will iterate over each element in the array and then yield to a  
supplied block with a single variable, the item in the array.  
If you have a look at the C code, you’ll see a familiar looking for  
loop with calls to yield.

For a better understanding, you need to do some further reading on  
blocks in ruby.

Start with [http://www.rubycentral.com/book/tut\_containers.html](http://www.rubycentral.com/book/tut_containers.html)

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)

[http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings

---

<div class="post-metadata">

**Author:** ![Neela\_megha\_shyam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47c2d/32.png) [@Neela\_megha\_shyam\_C](https://www.ruby-forum.com/u/Neela_megha_shyam_C)\
**Post date:** [May 23, 2009, 10:09am UTC](https://www.ruby-forum.com/t/how-does-array-each-element-work/168564/3 "2009-05-23T10:09:11Z")

</div>

Andrew T. wrote:

> On Fri, May 22, 2009 at 12:49 PM, Neela megha shyam Chivukula  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > O/p:-  
> > 1  
> > 2  
> > 3
> > 
> > I want to know how the array.each works. How the compiler interprets and  
> > prints.
> > 
> > Thanks and regards,  
> > Neela.
> 
> [http://www.ruby-doc.org/core/classes/SOAP/RPC/SOAPMethodResponse.html#M006286](http://www.ruby-doc.org/core/classes/SOAP/RPC/SOAPMethodResponse.html#M006286)  
> each will iterate over each element in the array and then yield to a  
> supplied block with a single variable, the item in the array.  
> If you have a look at the C code, you’ll see a familiar looking for  
> loop with calls to yield.
> 
> For a better understanding, you need to do some further reading on  
> blocks in ruby.
> 
> Start with [http://www.rubycentral.com/book/tut\_containers.html](http://www.rubycentral.com/book/tut_containers.html)
> 
> Andrew T.  
> [http://ramblingsonrails.com](http://ramblingsonrails.com)
> 
> [http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings

Hi Andrew,

I will go through the link. Thank you.

Thanks and regards,  
Neela.
