From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2002-11-10T10:03:25+09:00 Subject: Re: Lighting Rod In article <48674.148.78.249.10.1036859159.squirrel@enigo.com>, Kirk Haines wrote: >Eric Armstrong said: > >> 3) 3.times. >> How many times does this execute, really? >> I saw one page that said it cycled through >> indices 0,1,2,3. Hopefully, that was an error! >> (If not, this item definitely belongs on the list >> of things every newcomer needs to know!) > >This one is certainly easy to test for oneself: > >[root@enigo1 tutorial]# irb >irb(main):001:0> 3.times {|d| puts d} >0 >1 >2 >3 >irb(main):002:0> 2.times {|d| puts d} >0 >1 >2 >irb(main):003:0> 0.times {|d| puts d} >0 > >Note that the above is done with Ruby 1.6.7 > >And this is certainly inconsistent with what is stated in the Pickaxe book: > >"Iterates block int times, passing in values from zero to int - 1" Well, no it's not inconsistent. The last number you're seeing is returned from the block and irb prints it. Try this: irb(main):002:0> 3.times { |i| puts "iteration: #{i}" } iteration: 0 iteration: 1 iteration: 2 3 Phil