From: William James Date: 2007-09-12T16:20:11+09:00 Subject: Re: Is this a bug or a feature? On Sep 6, 2:00 pm, Martin Jansson wrote: > irb(main):002:0> i=1 > => 1 > irb(main):003:0> until (i+=1)>5 > irb(main):004:1> p i > irb(main):005:1> end > 2 > 3 > 4 > 5 > => nil > irb(main):006:0> RUBY_VERSION > => "1.8.3" > > The condition is evaluated before the first iteration. > > At least it's consistent: > > irb(main):005:0> i = 1 > => 1 > irb(main):006:0> p i until (i+=1)>5 > 2 > 3 > 4 > 5 > => nil > > I think it makes "until" less useful. Why would anyone want this? In Pascal, "until" differs from "while" in two ways: 1. "While" exits the loop when the condition is false; "until" exits when the condition is true. 2. "While" checks the condition at the top of the loop; "until" checks at the bottom of the loop. In Ruby, "until" differs from "while" in only one way: 1. "While" exits the loop when the condition is false; "until" exits when the condition is true. You could say that "until" is to "while" as "unless" is to "if". Ruby is more flexible than Pascal in that the both "while" and "until" can check the condition either at the top or the bottom of the loop. # Count to 5, checking at the top of the loop. i = 0 while (i += 1) <= 5 print i, " " end puts # Count to 5, checking at the top of the loop. i = 0 until not (i += 1) <= 5 print i, " " end puts # Count to 5, checking at the bottom of the loop. i = 1 begin print i, " " end while (i += 1) <= 5 puts # Count to 5, checking at the bottom of the loop. i = 1 begin print i, " " end until not (i += 1) <= 5 puts Now, what about this? p i until (i+=1) > 5 That is merely perlish shorthand for until (i+=1) > 5 p i end just as p i if i < 6 is perlish shorthand for if i < 6 p i end If that seems confusing, don't use it.