[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

continuing the next iteration

YAD

9/8/2006 3:50:00 AM

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
6 Answers

Paul Lutus

9/8/2006 4:14:00 AM

0

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

--
Paul Lutus
http://www.ara...

YAD

9/8/2006 6:42:00 AM

0

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

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

YAD

9/8/2006 6:50:00 AM

0

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

William James

9/8/2006 7:35:00 AM

0

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


def do_rows(grid, char)
grid.each {|row|
return true if row.all?{|c| c==char}
}
return nil
end

YAD

9/8/2006 4:26:00 PM

0

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

David Vallner

9/9/2006 11:33:00 AM

0

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