From: dblack@... Date: 2006-12-26T08:12:00+09:00 Subject: Re: Merry (slightly early) Christmas! Mr. Neighborly's Humble Little Ruby Book is free! ---2049402039-1218730451-1167088315=:12643 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1218730451-1167088315=:12643" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-1218730451-1167088315=:12643 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Tue, 26 Dec 2006, Jeremy McAnally wrote: > On 12/22/06, Tyler Prete wrote: >> This book is really great! I'm familiar with most of the material alrea= dy, >> 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: >>=20 >> def myeach(myarray) >> iter =3D 0 >> while (iter < myarray.length): >> yield(myarray[iter]) >> iter +=3D 1 >> end >> end >> testarray =3D [1,2,3,4,5] >> myeach(testarray) {|item| print "#{item}:"} >> =A2=AA 1:2:3:4:5: >>=20 >> Rather than using what seems (to me at least) the more Ruby way: >>=20 >> def myeach(myarray) >> for elem in myarray >> yield elem >> end >> end >>=20 >> That way you're not using an extra variable or having to worry about arr= ay >> length. >>=20 >> A funny way to write it would be using blocks: >> def myeach(myarray) >> myarray.each { |elem| yield(elem) } >> end >>=20 >> but I think that way basically defeats the purpose of your example ;) >>=20 >> Anyway, I'm just interested in your thoughts about this. You most likel= y >> have your reasons and they probably make sense; after all, you're the on= e >> out there writing books :) >>=20 > 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. :) I definitely agree that that -- dealing with forward topic references as one writes -- is a challenge. My favorite example is: you can't really explain Enumerable until you've explained arrays, but Array is Enumerable.... :-) It is, though, a good affirmation of the fact that Ruby is a nicely integrated system and a well-oiled machine. David --=20 Q. What's a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) ---2049402039-1218730451-1167088315=:12643-- ---2049402039-1218730451-1167088315=:12643--