From: Martin DeMello Date: 2006-12-24T10:14:33+09:00 Subject: Re: Merry (slightly early) Christmas! Mr. Neighborly's Humble Little Ruby Book is free! On 12/23/06, Tyler Prete wrote: > This book is really great! I'm familiar with most of the material already, > but I'm enjoying reading through it all the same. One question I have (a > question of preference more than correctness) is in your example dealing > with blocks and yield, why do you choose to use while with an interator: > > def myeach(myarray) > iter = 0 > while (iter < myarray.length): > yield(myarray[iter]) > iter += 1 > end > end > testarray = [1,2,3,4,5] > myeach(testarray) {|item| print "#{item}:"} > → 1:2:3:4:5: > > Rather than using what seems (to me at least) the more Ruby way: > > def myeach(myarray) > for elem in myarray > yield elem > end > end > > That way you're not using an extra variable or having to worry about array > length. > > A funny way to write it would be using blocks: > def myeach(myarray) > myarray.each { |elem| yield(elem) } > end > > but I think that way basically defeats the purpose of your example ;) "for i in collection" is converted by the parser to collection.each do |i|, so it comes to the same thing as your last example (: martin