Forum: Ruby posipele to jump to another line in the program?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D66b4fec61b64432eca2abfc638cf82d?d=identicon&s=25 Johannes Fredborg (mime)
on 2007-01-02 19:07
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!
Ff9e18f0699bf079f1fc91c8d4506438?d=identicon&s=25 James Britt (Guest)
on 2007-01-02 19:15
(Received via mailing list)
Johannes Fredborg 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/...

http://www.ruby-doc.org/docs/ProgrammingRuby/html/...


throw/catch might be the better fit.

--
James Britt

"The greatest obstacle to discovery is not ignorance, but the illusion
of knowledge."
  - D. Boorstin
05e48e632fdd0b2c25d27042f52c11d5?d=identicon&s=25 Alex LeDonne (Guest)
on 2007-01-02 19:37
(Received via mailing list)
On 1/2/07, Johannes Fredborg <johannes_room@hotmail.com> 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
...
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-01-02 19:41
(Received via mailing list)
Johannes Fredborg 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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-02 20:35
(Received via mailing list)
Johannes Fredborg 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
}
This topic is locked and can not be replied to.