From: Terry Poulin Date: 2007-09-08T06:01:16+09:00 Subject: Re: Is this a bug or a feature? >> >> I think what Martin wants is a bottom-tested loop, but I don't believe >> Ruby has that. until is a top-tested loop just like while. The only way I >> know to get bottom-tested behavior is this. >> >> loop do >> # some code goes here >> break if condition >> end > > You can do: > > begin > # do stuff > end until condition > > > David > irb(main):001:0> i = 0 => 0 irb(main):002:0> until (i+=1) >5 do irb(main):003:1* p i irb(main):004:1> end 1 2 3 4 5 => nil irb(main):005:0> Because i is incremented at each passage of the loop, _before_ it starts the loop if i is 1 when i+=1 is evaluated it becomes 2 when you try to print it. It's nether a bug nore a feature IMHO. If you *do* want to have a bottom tested loop much as David just said; irb(main):001:0> i = 1 => 0 irb(main):002:0> begin irb(main):003:1* puts i irb(main):004:1> end until (i+=1)>5 1 2 3 4 5 => nil irb(main):005:0> Works fine with i equal to 1 before starting the loop -> because it prints i before it is changed when i+=1 is evaluated before the second pass through the block is made. If you do find a need to use a test at the bottom instead of the top, you should ether 0.) Keep the body of the loop short or 1.) Make it explicit to the reader that a loop is starting. Generally I avoid code like that because If six months later you don't know (or that a loop is starting until you see the until expression, it's more trouble then following a more conventional style. If you ask me, if the body of a loop grows to long that you don't remember why the loop was executing when you hit the end of it, thats a problem in of it self any way ;-) TerryP. -- Email and shopping with the feelgood factor! 55% of income to good causes. http://www.ippimail.com