From: mark sparshatt Date: 2005-03-31T04:41:03+09:00 Subject: Re: beginner iterators question Jeff Fry wrote: > Hi, > > I am brand new to ruby and liking it so far - particularly the watir > test libraries. I am comfortable with the Java for loop syntax (i=0, > i<4, i++) and haven't found something in ruby that feels as elegant, > particularly when I want to use an increasing value i within the loop. > Here's an example (with my limited ruby) > > i=0 > 0.upto(3) do puts 'try > number ' + i.to_s i = > i+1 # is there a more elegant way to > do i++ ? > end > how about this 0.upto(3) do |i| puts 'try number ' + i.to_s end the upto method passes the current value into the block when calling it. also another way of writing i++ is i += 1 > I am guessing that ruby provides a more elegant way to do the above. I > would love recommendations both on how to iterate when I want to use the > changing value of the iterator in the loop, and how in ruby to do i++. > Is there a way with upto() to use the current value during iteration? or > another iterator where that is possible without needing to define i > seperately? > hth -- Mark Sparshatt