From: 7stud -- Date: 2007-10-20T12:28:16+09:00 Subject: Re: iterating through a block; declaring an index/counter within that block Thufir wrote: > Where does "food" come from in the example below? The declaration is > within the block? The varaible food exists only within the block? > What is (are) the value(s) for "food"? > > > > > Read the following aloud to yourself. > > ['toast', 'cheese', 'wine'].each { |food| print food.capitalize } > That thing on the right of each, between the braces, is a similar to a method definition: def func(food) print food.capitalize end food is the parameter variable and you would call func like this: func('toast') 'toast' then gets assigned to the parameter variable food. each() is also method. each() is defined to pass each member of the thing to its left to the block on its right. It is similar to doing this: def each(arr) count = 0 while count < arr.length curr_elmt = arr[count] my_func(curr_elmt) count += 1 end end def my_func(food) print food.capitalize end arr = ['toast', 'cheese', 'wine'] each(arr) -- Posted via http://www.ruby-forum.com/.