# How to use "goto" method inruby?

**URL:** <https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611>\
**Category:** Ruby\
**Created:** [December 19, 2007, 5:08am UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611 "2007-12-19T05:08:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 19, 2007, 5:08am UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/1 "2007-12-19T05:08:17Z")

</div>

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>

```

Thanks in advance,  
Martin

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 19, 2007, 5:32am UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/2 "2007-12-19T05:32:13Z")

</div>

On Dec 18, 2007 11:08 PM, dare ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 19, 2007, 5:34am UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/3 "2007-12-19T05:34:34Z")

</div>

This was thunk up way back in 2003:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/72922](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/72922)

(Its amazing what google can turn up).

Of course this does beget the question: why would you ever want to??

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 19, 2007, 8:45am UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/4 "2007-12-19T08:45:41Z")

</div>

On 19.12.2007 05:08, dare ruby wrote:

> 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>
> 
> ```

I do know nothing about this assignment of yours but I seriously  
question the competence of the person that required you to use “goto”.  
You can read up tons of arguments out there on the web and even on this  
list against using this form of ancient flow control.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 22, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/5 "2007-12-22T15:03:58Z")

</div>

On Dec 19, 2007, at 1:45 AM, Robert K. wrote:

> “goto”. You can read up tons of arguments out there on the web and  
> even on this list against using this form of ancient flow control.
> 
> Kind regards
> 
> robert

And there you can start to see part of why the assignment is to use  
goto in different languages…

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 22, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/6 "2007-12-22T18:30:37Z")

</div>

On 22.12.2007 15:03, John J. wrote:

> > question the competence of the person that required you to use  
> > “goto”. You can read up tons of arguments out there on the web and  
> > even on this list against using this form of ancient flow control.
> 
> And there you can start to see part of why the assignment is to use  
> goto in different languages…

You mean, someone is trying to teach the awfulness of “goto” by making  
pupils use it until it hurts? Interesting approach… 🙂

Cheers

robert

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 23, 2007, 12:01am UTC](https://www.ruby-forum.com/t/how-to-use-goto-method-inruby/123611/7 "2007-12-23T00:01:34Z")

</div>

On 22 Dec, 14:03, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> And there you can start to see part of why the assignment is to use  
> goto in different languages…

Perhaps it’s the CS equivalent of asking the new guy to fetch a left-  
handed screwdriver?

has (still looking for that glass hammer…)
