Forum: Ruby How does array.each element work?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fba26585183a418cdd5525fc632a1d1b?d=identicon&s=25 Neela megha shyam Chivukula (indrashyam)
on 2009-05-22 12:49
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.
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-05-22 13:13
(Received via mailing list)
On Fri, May 22, 2009 at 12:49 PM, Neela megha shyam Chivukula
<indrashyam@gmail.com> 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/SOAP...
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

Andrew Timberlake
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings
Fba26585183a418cdd5525fc632a1d1b?d=identicon&s=25 Neela megha shyam Chivukula (indrashyam)
on 2009-05-23 10:09
Andrew Timberlake wrote:
> On Fri, May 22, 2009 at 12:49 PM, Neela megha shyam Chivukula
> <indrashyam@gmail.com> 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/SOAP...
> 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
>
> Andrew Timberlake
> http://ramblingsonrails.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.
This topic is locked and can not be replied to.