From: "jwmerrill@..." Date: 2008-03-03T23:29:59+09:00 Subject: Re: Normal For Loop On Mar 3, 9:10 am, Cory Cory wrote: > I'm new to Ruby. Is there such thing as a C++ style (and most others) > for loop in Ruby? What I mean by that is: > > for( init, while-condition, increment ) { function-here } for n in 1..10 puts n end It seems more common in ruby to do things like 1.upto(10) {|n| puts n} or (1..10).each {|n| puts n} If you mean that you want your condition and your increment to depend on things that you won't know about until after the loop has been running for a while, then yeah, you might have to use a while loop. This doesn't seem to come up much, though. Perhaps you could post a specific example. > I know I can do that with a while loop, but I enjoy the compactness of > the above statement. There are many times where Ruby's for-each loops > just don't do the trick. With a little creativity, not so many, in my experience. > Also, is there anything like i++ or do I always have to do i=i+1 ? i=i+1 is the way to do it. There is some rationale for this, but I don't remember what it is. JM