# Posipele to jump to another line in the program?

**URL:** <https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978>\
**Category:** Ruby\
**Created:** [January 2, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978 "2007-01-02T19:07:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hanez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hanez](https://www.ruby-forum.com/u/hanez)\
**Post date:** [January 2, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978/1 "2007-01-02T19:07:48Z")

</div>

Okay guys!  
I am pritty new to Ruby programing and i need your help.

Is it possibel to ask the program to go back to another program line  
number and then proceed from there?

an exampele:

ans1 = gets.chomp \<----------------- (proceed from here)  
if ans1 == ‘black’  
co1 = 0  
puts  
puts ‘type in the next color’  
ans2 = gets.chomp  
end

if ans1 == ‘brown’  
co1 = 1  
puts  
puts ‘type in the next color’  
ans2 = gets.chomp  
end

if co1 == nil  
puts  
puts ‘Not acepted, please retype the color’  
goto.line 1 \<------------------------------ (this was just a wild wild  
guess)  
end  
…  
…

Do you see what i mean? is this possibel to do. Or is there another way  
that I can achieve the same effect?

Any help? Thanks!

---

<div class="post-metadata">

**Author:** ![hanez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hanez](https://www.ruby-forum.com/u/hanez)\
**Post date:** [January 2, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978/2 "2007-01-02T19:15:16Z")

</div>

Johannes F. wrote:

> Okay guys!  
> I am pritty new to Ruby programing and i need your help.
> 
> Is it possibel to ask the program to go back to another program line  
> number and then proceed from there?

Take a look at continuations, and throw/catch

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref\_c\_continuation.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_continuation.html)

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut\_exceptions.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html)

throw/catch might be the better fit.

–  
James B.

“The greatest obstacle to discovery is not ignorance, but the illusion  
of knowledge.”

- D. Boorstin

---

<div class="post-metadata">

**Author:** ![hanez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hanez](https://www.ruby-forum.com/u/hanez)\
**Post date:** [January 2, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978/3 "2007-01-02T19:37:26Z")

</div>

On 1/2/07, Johannes F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> co1 = 0  
> end  
> Do you see what i mean? is this possibel to do. Or is there another way  
> that I can achieve the same effect?
> 
> Any help? Thanks!

Johannes-

Here’s a hint as to how you can do this without a “goto”. It looks to  
me like you want to keep prompting as long as you haven’t set co1,  
right? See if you can fill in the … blanks below to get what you’re  
aiming for.

-Alex

co1 = nil

while co1.nil? do  
ans1 = gets.chomp  
case ans1  
when ‘black’  
…  
when …  
…  
else  
…  
end  
end

# now that we know we got co1, let’s get the next color

puts ‘type the next color’  
ans2 = gets.chomp  
…

---

<div class="post-metadata">

**Author:** ![hanez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hanez](https://www.ruby-forum.com/u/hanez)\
**Post date:** [January 2, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978/4 "2007-01-02T20:35:43Z")

</div>

Johannes F. wrote:

> co1 = 0  
> end
> 
> if co1 == nil  
> puts  
> puts ‘Not acepted, please retype the color’  
> goto.line 1 \<------------------------------ (this was just a wild wild  
> guess)  
> end

co1 = false  
begin  
ans1 = gets.chomp  
case ans1  
when ‘black’  
co1 = 0  
when ‘brown’  
co1 = 1  
else  
puts  
puts ‘Not acepted, please retype the color’  
end  
end until co1  
puts  
puts ‘type in the next color’  
ans2 = gets.chomp

— Another way. -----

choice\_set = [%w(black brown), %w(blue green)]  
colors = []  
choice\_set.each{|choices|  
begin  
puts “Type one of these colors:”  
puts choices  
num = choices.index( gets.chomp ) or  
puts ‘’, ‘Not acepted, please retype the color’  
end until num  
colors \<\< num  
}

---

<div class="post-metadata">

**Author:** ![hanez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5e925/32.png) [@hanez](https://www.ruby-forum.com/u/hanez)\
**Post date:** [January 2, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/posipele-to-jump-to-another-line-in-the-program/80978/5 "2007-01-02T19:41:27Z")

</div>

Johannes F. wrote:

> co1 = 0  
> end  
> Do you see what i mean? is this possibel to do. Or is there another way  
> that I can achieve the same effect?

One way is to use the “redo” keyword. However, it only works within a  
do-end block. Here’s an example:

$ cat redo.rb  
1.times do  
print "input: "  
ans = gets.chomp  
if ans == ‘foo’  
puts “bad input: #{ans}”  
redo  
end  
puts “good input: #{ans}”  
end

$ ruby redo.rb  
input: foo  
bad input: foo  
input: foo  
bad input: foo  
input: bar  
good input: bar

It is a bit strange to go into a “1.times” loop just so that we can use  
the “redo” keyword. (Actually, we could use a while-end block, but that  
would be awkward too.) If you like, you can write your own iterator in  
place of #times, like so:

def in\_redo\_context  
yield  
end

in\_redo\_context do  
print "input: "  
ans = gets.chomp  
if ans == ‘foo’  
puts “bad input: #{ans}”  
redo  
end  
puts “good input: #{ans}”  
end

This approach uses a block for each line you want to return to. So this  
will start to get messy if you have lots different place to return to,  
since you will need nested blocks. In that case, you might want to  
consider James’s suggestion of continuations.

Another approach is to use exceptions:

class BadInput \< StandardError; end

begin  
print "input: "  
ans = gets.chomp  
if ans == ‘foo’  
raise BadInput, “bad input: #{ans}”  
end  
puts “good input: #{ans}”

rescue BadInput =\> ex  
puts ex.message  
retry  
end
