[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is this a bug or a feature?

Martin Jansson

9/6/2007 7:00:00 PM

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-....

11 Answers

Wilson Bilkovich

9/6/2007 7:16:00 PM

0

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://rubyu...

mrpink

9/6/2007 7:31:00 PM

0

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://rubyu...
>


--
kazaam <kazaam@oleco.net>

Jeremy Woertink

9/6/2007 7:51:00 PM

0

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-....


Ezra Zygmuntowicz

9/6/2007 7:55:00 PM

0


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?


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)



dblack

9/6/2007 7:58:00 PM

0

Robert Klemme

9/6/2007 9:17:00 PM

0

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?

def print_all(arr)
until arr.empty?
print ">", arr.shift, "<\n"
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

robert

Terry Poulin

9/7/2007 9:01:00 PM

0

>>
>> 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.ip...


Martin Jansson

9/11/2007 10:26:00 PM

0

unknown wrote:
> Hi --
>
> On Fri, 7 Sep 2007, Mark Volkmann wrote:
>
>>>> 3
>>>> irb(main):005:0> i = 1
>>> What other behavior would you want, and why?
>> end
> You can do:
>
> begin
> # do stuff
> 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
# 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.
--
Posted via http://www.ruby-....

Phrogz

9/11/2007 10:51:00 PM

0

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".

Consider this:
if foo() then
bar()
end

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?

William James

9/12/2007 7:15:00 AM

0

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?

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.