Forum: Ruby "Goto line" function?

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.
2fe2857508642f1cb0963e49bfa89001?d=identicon&s=25 Helgitomas Gislason (helgso)
on 2007-03-09 22:58
Ok, I've read through the forum but I haven't got my answer. If a value
in my program is true, I want my program to go back to a line like this:

(Example of the program)

1.  puts ' '
2.  puts ' '
3.  puts '****************************'
4.  puts '** (Rules of the program) **'
5.  puts '****************************'
6.  puts ' '
7.  puts 'Ohh, hello there! What are you named many names?'
8.
9.  names = gets.chomp
10.
11. if names == '2'
12.   puts ' '
13.   puts 'Are you named two names?'
14.   puts ' '
15.   answer = gets.chomp
16.   if answer == 'yes'
17.     (the program continues)
18.
19. BUT if answer == 'no'
20.       goto.line.7 (And skip the rules, just restart it and begin
from line 7.)

What is the command? I know it's not "goto.line.X" but what is it? "back
to line.X"? "goto.lineX"? What is the command?
7b4707f974af261f71943e1f2046c9ee?d=identicon&s=25 SonOfLilit (Guest)
on 2007-03-09 23:07
(Received via mailing list)
You don't want to do this. This is an old bad practice from BASIC days
that is thankfully almost forgotten.

You want a while loop around lines 7-20.

In other cases there are other workarounds around goto.

If you must goto, there is a hack with call//cc. I needed it once, you
probably won't. Ever.

Aur

Check RubyMentor at http://rubymentor.rubyforge.org/
8912aa610b5a39c0749dc1718c797467?d=identicon&s=25 Paul van Delst (Guest)
on 2007-03-10 00:40
(Received via mailing list)
Helgitomas Gislason wrote:
> 6.  puts ' '
> 17.     (the program continues)
> 18.
> 19. BUT if answer == 'no'
> 20.       goto.line.7 (And skip the rules, just restart it and begin
> from line 7.)
>
> What is the command? I know it's not "goto.line.X" but what is it? "back
> to line.X"? "goto.lineX"? What is the command?

Addressing just the loop/goto issue, how about summat like:

#!/usr/bin/env ruby
done=false
until done
   puts ' '
   puts ' '
   puts '****************************'
   puts '** (Rules of the program) **'
   puts '****************************'
   puts ' '
   puts 'Ohh, hello there! What are you named many names?'
   names = gets.chomp
   if names == '2'
     puts ' '
     puts 'Are you named two names?'
     puts ' '
     answer = gets.chomp
     done = true if answer == 'yes'
   end
end
#This is where "the program continues"
puts 'the end'


This gives me the output:

lnx:scratch : blah.rb


****************************
** (Rules of the program) **
****************************

Ohh, hello there! What are you named many names?
2

Are you named two names?

no


****************************
** (Rules of the program) **
****************************

Ohh, hello there! What are you named many names?
2

Are you named two names?

yes
the end




cheers,

paulv
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-03-10 15:41
(Received via mailing list)
On Sat, Mar 10, 2007 at 07:06:25AM +0900, SonOfLilit wrote:
> If you must goto, there is a hack with call//cc. I needed it once, you
> probably won't. Ever.

Less mind-bending is catch/throw, or 'redo' within a block.

If you *really* want to write BASIC programs in Ruby, this was discussed
several years ago in ruby-talk - search the archives :-)
0a3117ace9ffd686054dfec667e9a976?d=identicon&s=25 Albert Ng (Guest)
on 2007-09-25 23:01
(Received via mailing list)
don't blame him, I'm still learning TASM and GOTO's just like JMP
This topic is locked and can not be replied to.