Loop/Iterator questions

G

GGarramuno

1) Is there anything like Perl's continue block available? This is
not next, but a block that gets executed on each loop, before the
condition is tested, similar to the third block of a for loop does.
Ex:
i = 10
while i > 0
# do stuff
x = rand
if x > 0.5 next
p "not always get here"
continue
i -= 1
end

In the above example, i -= 1 gets executed at the end of the while
loop or if next is called.


2) Is there anything like labels for iterators or loops? This allows
you to do next, last, etc. on outside loops, like:

loop1: while x > 1
while y > 1
if t2 then next loop1
end
end

This could be emulated with a flag and an ugly if check, but I hope
not.
 
G

Gavin Sinclair

1) Is there anything like Perl's continue block available? This is not
next, but a block that gets executed on each loop, before the
condition is tested, similar to the third block of a for loop does. Ex:
i = 10
while i > 0
# do stuff
x = rand
if x > 0.5 next
p "not always get here"
continue
i -= 1
end

In the above example, i -= 1 gets executed at the end of the while loop
or if next is called.

This oughta do it:

i = 10
while i > 0
begin
# do stuff
if (x = rand) > 0.5 then next
puts "not always get here"
ensure
i -= 1
end
end

Gavin
 
G

GGarramuno

That sort of nonlocal control flow is done with catch/throw in Ruby.

catch:)done) do
while x > 1
while y > 1
if t2 then throw :done
end
end
end

Hmm... that does not seem quite correct, thou.

The throw() example above actually breaks from both loops. That's not
the same as next, thou.

Given pseudo-code, like:

catch:)arg) do
while $stdin.getc != "a"
begin
p "A"
while 1
p "B"
throw :arg if 1
end
ensure
#do a lot of stuff here
p "ensure"
end
end
end

In the dummy example above, I want the loop to continue (thus, getting
a character, and printing "A","B","ensure" until "a" is typed).
The throw seems to just exit the loop. If the catch is placed inside
the while, the getc condition is not run, then.

What's the right way?
 
G

GGarramuno

ts said:
G> while $stdin.getc != "a"

getc return a Fixnum, not a String

Guy Decoux

Thanks for mentioning that. That could indeed be some future gotcha I
could run into.

Still, that is not dealing with the flow problem I have.

Testing it now with:

catch:)arg) do
while $stdin.getc
begin
p "A"
while 1
p "B"
throw :arg if 1
end
ensure
#do a lot of stuff here
p "ensure"
end
end
end

and I still exit the loop right away. I want to be able to do a
"next" on the outer loop from within the inner loop and the
throw/catch mechanism seems to be only a solution to breaking out of
loops, not iterating to the next step (while evaluating the
condition).
I'm sure I am missing some obvious way of doing this.
 
J

Jamis Buck

GGarramuno said:
catch:)arg) do
while $stdin.getc
begin
p "A"
while 1
p "B"
throw :arg if 1
end
ensure
#do a lot of stuff here
p "ensure"
end
end
end

and I still exit the loop right away. I want to be able to do a
"next" on the outer loop from within the inner loop and the
throw/catch mechanism seems to be only a solution to breaking out of
loops, not iterating to the next step (while evaluating the
condition).
I'm sure I am missing some obvious way of doing this.

I think you just need to put the catch:)arg) inside the outer loop, like
this:

while $stdin.getc
catch:)arg) do
begin
p "A"
while true
p "B"
throw :arg if true
end
ensure
#do a lot of stuff here
p "ensure"
end
end
end

Another thing to watch for -- avoid treating '1' as true. Although '1'
is true, so is '0', in Ruby. Use the 'true' and 'false' literals, instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top