# Is this a bug or a feature?

**URL:** <https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436>\
**Category:** ruby-talk\
**Created:** [6 September 2007 19:00 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436 "2007-09-06T19:00:28Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Jansson](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Martin\_Jansson](https://rubytalk.org/u/Martin_Jansson)\
**Post date:** [6 September 2007 19:00 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/1 "2007-09-06T19:00:28Z")

</div>

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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [6 September 2007 19:16 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/2 "2007-09-06T19:16:13Z")

</div>

What other behavior would you want, and why?  
Here are some specs for the current behavior of 'until'. They may help.  
[http://rubyurl.com/12y](http://rubyurl.com/12y)

> **···**
>
> On 9/6/07, Martin Jansson \<martialis@bigfoot.com\> 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?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Woertink1](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Jeremy\_Woertink1](https://rubytalk.org/u/Jeremy_Woertink1)\
**Post date:** [6 September 2007 19:51 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/3 "2007-09-06T19:51:02Z")

</div>

I would say this is a feature to answer you question.  
Just think of what gets printed when you do  
i = 1  
i = i + 1

so you are saying  
until (i = i + 1) \> 5  
p i  
end

the second time the i is evaluated is in the until loop.

~Jeremy

> **···**
>
> On Sep 6, 12:00 pm, Martin Jansson \<martia...@bigfoot.com\> 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?  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ezra\_Zygmuntowicz](https://rubytalk.org/u/Ezra_Zygmuntowicz)\
**Post date:** [6 September 2007 19:55 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/4 "2007-09-06T19:55:28Z")

</div>

If you want a bottom tested loop then do it this way:

irb(main):006:0\> i=1  
=\> 1  
irb(main):007:0\> begin  
irb(main):008:1\* p i  
irb(main):009:1\> end until (i+=1) \> 5  
1  
2  
3  
4  
5  
=\> nil  
irb(main):010:0\>

Cheers-

-- Ezra Zygmuntowicz -- Founder & Ruby Hacker  
-- ez@engineyard.com  
-- Engine Yard, Serious Rails Hosting  
-- (866) 518-YARD (9273)

> **···**
>
> On Sep 6, 2007, at 12: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?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 September 2007 21:20 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/5 "2007-09-06T21:20:06Z")

</div>

def print\_all(arr)  
&nbsp;&nbsp;&nbsp;until arr.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\>", arr.shift, "\<\n"  
&nbsp;&nbsp;&nbsp;end  
end

Try this with post checked conditions and an empty array as argument.  
Language designers seem to think otherwise. You described the logic of loops with prechecked conditions which abound in programming languages - for decades already.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 06.09.2007 21:00, 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?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [12 September 2007 07:20 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/6 "2007-09-12T07:20:11Z")

</div>

In Pascal, "until" differs from "while" in two ways:

&nbsp;&nbsp;1. "While" exits the loop when the condition is false;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"until" exits when the condition is true.  
&nbsp;&nbsp;2. "While" checks the condition at the top of the loop;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"until" checks at the bottom of the loop.

In Ruby, "until" differs from "while" in only one way:

&nbsp;&nbsp;1. "While" exits the loop when the condition is false;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"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  
&nbsp;&nbsp;print i, " "  
end  
puts

# Count to 5, checking at the top of the loop.  
i = 0  
until not (i += 1) \<= 5  
&nbsp;&nbsp;print i, " "  
end  
puts

# Count to 5, checking at the bottom of the loop.  
i = 1  
begin  
&nbsp;&nbsp;print i, " "  
end while (i += 1) \<= 5  
puts

# Count to 5, checking at the bottom of the loop.  
i = 1  
begin  
&nbsp;&nbsp;print i, " "  
end until not (i += 1) \<= 5  
puts

Now, what about this?

&nbsp;&nbsp;p i until (i+=1) \> 5

That is merely perlish shorthand for

&nbsp;&nbsp;until (i+=1) \> 5  
&nbsp;&nbsp;&nbsp;&nbsp;p i  
&nbsp;&nbsp;end

just as

&nbsp;&nbsp;p i if i \< 6

is perlish shorthand for

&nbsp;&nbsp;if i \< 6  
&nbsp;&nbsp;&nbsp;&nbsp;p i  
&nbsp;&nbsp;end

If that seems confusing, don't use it.

> **···**
>
> On Sep 6, 2:00 pm, Martin Jansson \<martia...@bigfoot.com\> 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?

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [12 September 2007 10:37 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/7 "2007-09-12T10:37:21Z")

</div>

Here is what I understand based on Pascal.

\<waits for the laughing to stop\>

There are two main kinds of this loop:

repeat..until

while..do

In the repeat..until (our own version of the until) it does things then  
checks for the break condition. In the while loop, it checks then does  
things.

In our example here, we have:

i=1  
until (i+=1)\>5  
&nbsp;&nbsp;p i  
end

The question is, when does it evaluate? When I learned basic algebra, I  
was taught to figure out the things inside the parens first. I think  
that is what is done here. Therefore, the first thing done is to  
increment, then evaluate.

To get what you seem to want, you would need to evaluate before you  
increment, which would look more like this:

i = 1  
until i\>5  
&nbsp;&nbsp;p i  
&nbsp;&nbsp;i+=1  
end

You were telling it to increment, THEN evaluate. That being the case,  
it would not be a bug. IMHO

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mark\_Volkmann](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mark\_Volkmann](https://rubytalk.org/u/Mark_Volkmann)\
**Post date:** [6 September 2007 19:29 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/8 "2007-09-06T19:29:41Z")

</div>

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  
&nbsp;&nbsp;&nbsp;# some code goes here  
&nbsp;&nbsp;&nbsp;break if condition  
end

> **···**
>
> On Sep 6, 2007, at 2:16 PM, Wilson Bilkovich wrote:
> 
> > On 9/6/07, Martin Jansson \<martialis@bigfoot.com\> 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?
> > 
> > What other behavior would you want, and why?  
> > Here are some specs for the current behavior of 'until'. They may help.  
> > [http://rubyurl.com/12y](http://rubyurl.com/12y)
> 
> ---  
> Mark Volkmann

---

<div class="post-metadata">

**Author:** ![anansi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anansi](https://rubytalk.org/u/anansi)\
**Post date:** [6 September 2007 19:35 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/9 "2007-09-06T19:35:04Z")

</div>

I think I means why the 1 is not printed like in other langues if you do this with repeat...until

> **···**
>
> On Fri, 7 Sep 2007 04:16:13 +0900 "Wilson Bilkovich" \<wilsonb@gmail.com\> wrote:
> 
> > What other behavior would you want, and why?  
> > Here are some specs for the current behavior of 'until'. They may help.  
> > [http://rubyurl.com/12y](http://rubyurl.com/12y)
> 
> --  
> kazaam \<kazaam@oleco.net\>

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 September 2007 19:57 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/10 "2007-09-06T19:57:52Z")

</div>

Hi --

> **···**
>
> On Fri, 7 Sep 2007, Mark Volkmann wrote:
> 
> > On Sep 6, 2007, at 2:16 PM, Wilson Bilkovich wrote:
> > 
> > > On 9/6/07, Martin Jansson \<martialis@bigfoot.com\> 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?
> > > 
> > > What other behavior would you want, and why?  
> > > Here are some specs for the current behavior of 'until'. They may help.  
> > > [http://rubyurl.com/12y](http://rubyurl.com/12y)
> > 
> > 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:
> 
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;# do stuff  
> &nbsp;&nbsp;end until condition
> 
> David
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Terry\_Poulin](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@Terry\_Poulin](https://rubytalk.org/u/Terry_Poulin)\
**Post date:** [7 September 2007 21:01 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/11 "2007-09-07T21:01:16Z")

</div>

> > 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:
> 
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;# do stuff  
> &nbsp;&nbsp;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.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> Email and shopping with the feelgood factor!  
> 55% of income to good causes. [http://www.ippimail.com](http://www.ippimail.com)

---

<div class="post-metadata">

**Author:** ![Martin\_Jansson](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Martin\_Jansson](https://rubytalk.org/u/Martin_Jansson)\
**Post date:** [11 September 2007 22:25 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/12 "2007-09-11T22:25:52Z")

</div>

unknown wrote:

> Hi --
> 
> > > > 3  
> > > > irb(main):005:0\> i = 1
> > > 
> > > What other behavior would you want, and why?
> > 
> > end
> 
> You can do:
> 
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;# do stuff  
> &nbsp;&nbsp;end until condition
> 
> David

Thats just confusing. Why does that work different then "do\_stuff until  
condition".

I'm used to do things like this:  
a= n  
until (a\*= 3.0)\<=450.0  
&nbsp;&nbsp;&nbsp;# do stuff  
end

I think it is more readable. You get the start value, the incrementer  
and the stop condition nicely grouped together. You also get more  
effective code, if the penalty for evaluating the condition is high. I'm  
not a native english speaker, but I think it feels more in line with  
natural languages as well as "mathematical language". And it's a lot  
more like how you do things in real life.

Using the loop method would mean that I had to introduce a new scope.  
Which, sometimes, means that I have to remember to "declare" a lot of  
variables outside that scope. Even those that are never used. A lot of  
unecessary code attract bugs like sugar. I like Ruby's block iterators,  
but many times it is wrong to use them.

> **···**
>
> > On Fri, 7 Sep 2007, Mark Volkmann wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [11 September 2007 22:55 UTC](https://rubytalk.org/t/is-this-a-bug-or-a-feature/40436/13 "2007-09-11T22:55:09Z")

</div>

Consider this:  
&nbsp;&nbsp;if foo() then  
&nbsp;&nbsp;&nbsp;&nbsp;bar()  
&nbsp;&nbsp;end

&nbsp;&nbsp;bar() if foo()

In both cases foo() is called before bar(), even though in one of the  
cases it appears later in source code. The 'if' can go in different  
places. Similarly with while and until. Now, sometimes you (both the  
general populace and you in particular) want to evaluate after the  
first iteration. For such cases we need a different syntax.

Do you have a better suggestion for how to differentiate between  
evaluate-first and evaluate-after-one-iteration, that is also  
consistent with if/unless?

> **···**
>
> On Sep 11, 4:25 pm, Martin Jansson \<martia...@bigfoot.com\> wrote:
> 
> > \> You can do:
> > 
> > \> begin  
> > \> # do stuff  
> > \> end until condition
> > 
> > Thats just confusing. Why does that work different then "do\_stuff until  
> > condition".
