Michael W. Ryder wrote:
> Walton Hoops wrote:
> <snip>
> 
>> 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!  
> 
> 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 

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.)

> so this is one of those "magical"
> constructs much like your impression of ++ in C. 

No.  The "magic" is different.  In the Ruby version, a quick check in 
the API tells you that the counter is incremented each time through the 
loop.  In the C version, OTOH, you have to think about exactly where 
you've put the ++, and whether you really wanted the value before or 
after the increment.

> I would find this much
> harder to maintain than the C version.

Only because you apparently are not familiar with common Ruby idioms. 
The Ruby version has a lot less to go wrong in it, because the language 
transparently handles incrementing the "loop index" at the right time.


> 
> Which is part of the beauty of Ruby: It's simple,

Yes.  Just don't expect it to be much like C.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen / marnen.org
-- 
Posted via http://www.ruby-forum.com/.