What are the ways of executing a block from an iterator method?

Questions by nehalshah   answers by nehalshah

Showing Answers 1 - 3 of 3 Answers

Manu sharma

  • Sep 21st, 2012
 

Iterate and a block through an array :-

presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.each {|pres| puts pres}

OR

#The block neednt be on one line:

presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"]
presidents.each {
|pres|
puts pres
}

  Was this answer useful?  Yes

karmue

  • Apr 1st, 2013
 

when you can finish your code in one line, use {|x| ..... }
when you have multiple lines in a blocki, use
do |x|
....
end

  Was this answer useful?  Yes

Riturathin Sharma

  • Sep 28th, 2015
 

Code
  1. presidents.each do |president|

  2.   puts president

  3. end

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions