What's the Ruby equivalent of "next" in Perl or "continue" in C?
Is it possible to continue an outer loop, as in Perl's "next"
with a label argument?
···
--
Yet another Dan
What's the Ruby equivalent of "next" in Perl or "continue" in C?
Is it possible to continue an outer loop, as in Perl's "next"
with a label argument?
--
Yet another Dan
YAD wrote:
What's the Ruby equivalent of "next" in Perl or "continue" in C?
Is it possible to continue an outer loop, as in Perl's "next"
with a label argument?
You may or may not have heard this, but jumping to a label is frowned upon
in reliable, maintainable code. I don't find anything like "next" in Ruby,
but there is a "Continuation" class that can jump out of a complex nested
structure.
http://www.rubycentral.com/book/ref_c_continuation.html
--
Paul Lutus
http://www.arachnoid.com
Try catch/throw, although if you can rework your code into smaller
chunks and use a return it is often more readable.
catch (:deep_error) do
(0..10).each do |i|
puts i
(0..10).each do |j|
puts j
throw :deep_error if 2 == j
end
end
end
puts "done"
On 9/7/06, YAD <goofball@vapornet.com> wrote:
What's the Ruby equivalent of "next" in Perl or "continue" in C?
Is it possible to continue an outer loop, as in Perl's "next"
with a label argument?--
Yet another Dan
Paul Lutus wrote:
You may or may not have heard this, but jumping to a label is frowned upon
There's nothing wrong with a structured jump without a label.
btw, next is listed in the manual along with break, redo, and retry.
but there is a "Continuation" class that can jump out of a complex nested
structure.
http://www.rubycentral.com/book/ref_c_continuation.html
Well, it's a little on the clunky side, but it works.
def doRows (grid,char)
(0...3).each do |row|
callcc do |nextRow|
(0...3).each do |col|
nextRow.call() if grid[row][col] != char
end
return true
end
end
return false
end
Thanks for the help, I would have had a hard time finding that
on my own.
--
Yet another Dan
Sorry I did not read as closely as I should -- my example was just of
a "break," if you are after next/continue functionality, you need to
put the catch/throw inside the outer loop:
(0..10).each do |i|
puts i
catch :deep_error do
(0..10).each do |j|
puts j
(0..10).each do |k|
puts k
throw :deep_error if 2 == k
end
end
puts "never going to happen"
end
end
On 9/8/06, Patrick Hurley <phurley@gmail.com> wrote:
On 9/7/06, YAD <goofball@vapornet.com> wrote:
> What's the Ruby equivalent of "next" in Perl or "continue" in C?
> Is it possible to continue an outer loop, as in Perl's "next"
> with a label argument?
>
> --
> Yet another Dan
>Try catch/throw, although if you can rework your code into smaller
chunks and use a return it is often more readable.catch (:deep_error) do
(0..10).each do |i|
puts i
(0..10).each do |j|
puts j
throw :deep_error if 2 == j
end
end
end
puts "done"
YAD wrote:
callcc do |nextRow|
I wonder how much overhead there is associated with this
compared to the row.unique.size approach...
--
Yet another Dan
YAD wrote:
Paul Lutus wrote:
> You may or may not have heard this, but jumping to a label is frowned upon
There's nothing wrong with a structured jump without a label.btw, next is listed in the manual along with break, redo, and retry.
> but there is a "Continuation" class that can jump out of a complex nested
> structure.
> http://www.rubycentral.com/book/ref_c_continuation.htmlWell, it's a little on the clunky side, but it works.
def doRows (grid,char)
(0...3).each do |row|
callcc do |nextRow|
(0...3).each do |col|
nextRow.call() if grid[row][col] != char
end
return true
end
end
return false
end
def do_rows(grid, char)
grid.each {|row|
return true if row.all?{|c| c==char}
}
return nil
end
William James wrote:
> def do_rows(grid, char)
grid.each {|row|
return true if row.all?{|c| c==char}
}
return nil
end
Yes, that's the way to do it. Columns are still an issue,
but that looks like the most natural solution. Thanks.
--
Yet another Dan
YAD wrote:
YAD wrote:
callcc do |nextRow|
I wonder how much overhead there is associated with this
compared to the row.unique.size approach...
Amen. If you have extra data in the data set, I'd personally just first create a clean data set to process without jumps. And get rid of nested loops. And indulge in a lot more anal code cleanliness.
David Vallner