[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Idiom wanted: do-while

Adam Shelly

12/12/2005 10:35:00 PM

So I was working on the quiz solution, and
I had some code like this:

b = simulate board,m
while another_turn?(b,m)
b = simulate b,m
end


If I was doing this in C, I'd use a do-while loop instead, to avoid
repeating the line outside the loop:

b = board;
do {
b = simulate(b.m);
}
while ( another_turnta(b,m));

What's the do-while idiom in ruby?
I ended up with this, but it needs an extra flag variable:

b,taketurn = board,true
while taketurn
b = simulate b,m
taketurn = another_turn?(b,m)
end

Is there a ruby idiom for do-while?

-Adam


21 Answers

James Gray

12/12/2005 10:43:00 PM

0

On Dec 12, 2005, at 4:34 PM, Adam Shelly wrote:

> So I was working on the quiz solution, and
> I had some code like this:
>
> b = simulate board,m
> while another_turn?(b,m)
> b = simulate b,m
> end
>
>
> If I was doing this in C, I'd use a do-while loop instead, to avoid
> repeating the line outside the loop:
>
> b = board;
> do {
> b = simulate(b.m);
> }
> while ( another_turnta(b,m));
>
> What's the do-while idiom in ruby?
> I ended up with this, but it needs an extra flag variable:
>
> b,taketurn = board,true
> while taketurn
> b = simulate b,m
> taketurn = another_turn?(b,m)
> end
>
> Is there a ruby idiom for do-while?

I favor:

loop do
# ... some action ...
break unless ...
end

Hope that helps.

James Edward Gray II



Mauricio Fernández

12/12/2005 10:52:00 PM

0

On Tue, Dec 13, 2005 at 07:42:59AM +0900, James Edward Gray II wrote:
> >Is there a ruby idiom for do-while?
>
> I favor:
>
> loop do
> # ... some action ...
> break unless ...
> end

[ruby-core:06745]

--
Mauricio Fernandez


gabriele renzi

12/12/2005 11:08:00 PM

0

Adam Shelly ha scritto:
> So I was working on the quiz solution, and
> I had some code like this:
>
> b = simulate board,m
> while another_turn?(b,m)
> b = simulate b,m
> end
>
>
> If I was doing this in C, I'd use a do-while loop instead, to avoid
> repeating the line outside the loop:
>
> b = board;
> do {
> b = simulate(b.m);
> }
> while ( another_turnta(b,m));
>
> What's the do-while idiom in ruby?

you could do

begin
...
end while somecondition

but I seem to recall it is somewhat "deprecated" and you should follow
James' suggestion

> I ended up with this, but it needs an extra flag variable:
>
> b,taketurn = board,true
> while taketurn
> b = simulate b,m
> taketurn = another_turn?(b,m)
> end
>
> Is there a ruby idiom for do-while?
>
> -Adam
>
>

Andy Delcambre

12/12/2005 11:21:00 PM

0

You can also do
irb(main):011:0> begin
irb(main):012:1* puts "Once"
irb(main):013:1> end while nil
Once
=> nil

Which is what I usually do.

- Andy Delcambre


Ara.T.Howard

12/12/2005 11:37:00 PM

0

Brian Mitchell

12/12/2005 11:53:00 PM

0

On 12/12/05, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> harp:~ > cat a.rb
> i = 0
>
> (
> p i
> i += 1
> ) while i < 3
>
>
> harp:~ > ruby a.rb
> 0
> 1
> 2

Not quite. That is just a while loop:

( puts "foo" ) while nil
.. nil

which is as always, equivalent to:

while nil
puts "foo"
end


If you really need a do while loop it is not hard to use loop to
accomplish what you need. If you want something more complex you could
easily get this to work:

lambda do
...
end.while { ... }

All you would need is something like this:

class Proc
def while
loop do
result = call
return result unless yield
end
end
end

I am sure there is plenty of room for improving this example too.

Brian.


Adam Shelly

12/13/2005 12:20:00 AM

0

I was certain I had tried that....
But that works exactly the way I want.
Thanks,
-Adam

On 12/12/05, Andy Delcambre <adelcambre@gmail.com> wrote:
> You can also do
> irb(main):011:0> begin
> irb(main):012:1* puts "Once"
> irb(main):013:1> end while nil
> Once
> => nil
>
> Which is what I usually do.
>
> - Andy Delcambre
>
>


James Gray

12/13/2005 12:36:00 AM

0

On Dec 12, 2005, at 6:19 PM, Adam Shelly wrote:

> I was certain I had tried that....
> But that works exactly the way I want.

But hopefully you followed the Ruby Core link in this thread and saw
Matz explaining how he would like to remove it from the language so
we shouldn't be using it. Stick with loop do ... end.

James Edward Gray II


Steve Litt

12/13/2005 1:37:00 AM

0

On Monday 12 December 2005 05:42 pm, James Edward Gray II wrote:

> > Is there a ruby idiom for do-while?
>
> I favor:
>
> loop do
> # ... some action ...
> break unless ...
> end

I do this quite a bit, but it's not structured programming and is a little
like a goto. Break can improve readability on small loops, but on large loops
maintained by multiple people it can become a nightmare.

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


James Gray

12/13/2005 1:49:00 AM

0

On Dec 12, 2005, at 7:36 PM, Steve Litt wrote:

> On Monday 12 December 2005 05:42 pm, James Edward Gray II wrote:
>
>>> Is there a ruby idiom for do-while?
>>
>> I favor:
>>
>> loop do
>> # ... some action ...
>> break unless ...
>> end
>
> I do this quite a bit, but it's not structured programming and is a
> little
> like a goto. Break can improve readability on small loops, but on
> large loops
> maintained by multiple people it can become a nightmare.

Which is probably a sign that method calls are needed. Heck, I think
two programmers working on the same loop is a sign of that. ;)

James Edward Gray II