Brian Mitchell
12/19/2007 4:31:00 AM
On Dec 18, 2007 11:08 PM, dare ruby <martin@angleritech.com> wrote:
> Dear all,
>
> As a part of my task in some places, as like in other languages i was
> supposed to use "Goto" method. Is there any way to use goto in ruby?
>
> like in other languages,
>
> goto <labelno>
> goto <labelname>
>
There isn't a goto feature in Ruby, however, it isn't hard to get the
same sort of flow control out of more appropriate features. For loops
there is break, next, and redo. For general unwinding there are
exceptions (ie. begin-rescue/ensure/else) and catch-throw pairs (not
to be confused with exceptions). There are also delimited
continuations (via callcc), though I doubt you will need this feature.
Finally, what I recommend in most of these cases is refactoring. While
goto can be a very clear way to describe things, it is a rare
occasion. It is even more rare that another alternative ruby provides
isn't at least as clear.
If you can provide an idea of what you are thinking you need goto for,
go ahead and post some code and ruby-talk can hammer it out into a
beautiful master piece. If you still really want to have goto, I think
there is a library that emulates label based jumps... check RAA or
Rubyforge.
Brian.