How does array.each element work?

  • Thread starter Neela megha shyam Chivukula
  • Start date
N

Neela megha shyam Chivukula

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

Andrew Timberlake

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.


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

Andrew Timberlake
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings
 
N

Neela megha shyam Chivukula

Andrew said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top