From: John Joyce Date: 2008-01-12T09:57:46+09:00 Subject: Re: .times loop returns the number? On Jan 11, 2008, at 6:13 PM, Roger Pack wrote: > 12.times do |month_ahead| > end > > returns 12 > I would have expected this to return a collected array of the return > value of each block. Odd? > -- > Posted via http://www.ruby-forum.com/. > Not odd at all. x.times do |x| code_here_using_x end In this iterating construct the current value passed in | | is the current value of x. other than that, Ruby blocks always return the last value returned in the block. if you simply do this: x.times do |x| code_here_using_x puts "cat" end the block will return "cat" The iterating constructs in Ruby are not always used to return a value as in C-like languages. They simply perform the code in the block a number of times. This includes powerful and flexible concepts such iterating over collections (arrays, hashes, etc...) a = Array.new a.each do |element| some_function( element ) end The main idea on iterators in ruby is the iterating! But do keep in mind the simple convenience that Ruby code blocks implicitly return the last value in the block. a.each do |element| some_function( element ) 46 end returns 46. Sometimes that is useful and convenient.