Walton Hoops wrote: >> -----Original Message----- >> From: marnen / marnen.org [mailto:marnen / marnen.org] >>>> Now consider the ruby way: >>>> >>>> 10.times do |i| >>>> print "#{i}," >>>> end >>>> >>>> Some length as the C code, but much more readable. Heck, it's >>>> almost English! >> Well, you do have to know what Numeric#times yields to its block. But >> that's easy to look up. (However, it starts from 0, so it's not quite >> equivalent to the C.) > > Whoop! Good point, that's what I get for not actually testing my code. > > Corrected (and even closer to English). > > (1..9).each do |i| > print "#{i}," > end > That version I understand just looking at it as it is equivalent to a for loop. Your first version seemed more "magical" since I don't know where 'i' is getting incremented. At least with C I know where the incrementing is occurring. > Michael Wrote: >>> Not for me it wasn't. I had to try it to see that it actually works. >>> My initial impression was that it would print 10 copies of i. >> I still don't see where 'i' is incremented > > It isn't incremented, at least not in MY code (if you must think in > terms of incrementing variables, then Ruby is incrementing it for me) > See the times method at: > http://ruby-doc.org/core/classes/Integer.html > That kind of looping is a pretty core Ruby concept. > > Also the corrected version I wrote above should be a bit clearer. Also > I think your previous programming experience is hurting you here. To you > as (I'm guessing) a C programmer, to progress in a loop, you must increment > modify variable. The average English speaker doesn't think in those terms. > I started with Fortran in the early 1980's, followed by Basic, Pascal, Modula 2, and C. For over 25 years I have been mostly programming in Business Basic. While Ruby has a lot of things going for it I miss some of the features available in the other languages, especially the built in curses and file handling in Business Basic. > Looking at my most recent example, the English equivalent would be for each > 'i' from 1 to 9 print 'i' followed by a comma. Sure, the words may not be > in the precise order, but it comes a darn site closer to natural language than: > > int i=1; > while (i<10) > { > printf("%d,",i++); > } > > >