From: Jeremy McAnally Date: 2006-12-26T07:46:57+09:00 Subject: Re: Merry (slightly early) Christmas! Mr. Neighborly's Humble Little Ruby Book is free! Hey Tyler, While thats the most Rubyish way to do it, at that point in the text I hadn't covered those sorts of loops/iterators yet and thought that the while method would be more familiar to those reading. I may change it in the next revision, but I'm not sure. :) --Jeremy On 12/22/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 ;) > > Anyway, I'm just interested in your thoughts about this. You most likely > have your reasons and they probably make sense; after all, you're the one > out there writing books :) > > Thanks and keep up the good work, > Tyler Prete > >