goto function?

F

fabsy

Hey!
I have been trying to find a goto function in ruby..
Like this:

foo: bar
If blabla then GOTO foo

Is there any command for exiting the app?
And how do I search ex a textfile for a certain word and delete it?


------

if value == "2"
system('clear')
print "[Opening file..]\n\n"
r_file = File.open( "file", "a") #Wrote this so I wouldn't get an
error like "File doesn't exist".
r_file = File.open( "file", "r")
print "-----\n"
print r_file.read
print "-----"
print "\n"
system ('ruby note.rb') #is there anyway to restart or goto the
beginning of the script?
end
 
D

darren kirby

--nextPart1349417.xmmVqDXbTi
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

quoth the fabsy:
Hey!
I have been trying to find a goto function in ruby..
Like this:

foo: bar
If blabla then GOTO foo

Use a function:

def foo
#whatever foo does

foo() if blabla

or invert the test:

foo() unless !blabla
Is there any command for exiting the app?

Yeah: 'exit'
And how do I search ex a textfile for a certain word and delete it?

Probably lots of ways. One would be to read the file to a string and use=20
gsub(). See the String and IO docs
http://www.whytheluckystiff.net/ruby/pickaxe/html/builtins.html
------

if value =3D=3D "2"
system('clear')
print "[Opening file..]\n\n"
r_file =3D File.open( "file", "a") #Wrote this so I wouldn't get an
error like "File doesn't exist".
r_file =3D File.open( "file", "r")
print "-----\n"
print r_file.read
print "-----"
print "\n"
system ('ruby note.rb') #is there anyway to restart or goto the
beginning of the script?
end

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

--nextPart1349417.xmmVqDXbTi
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.4 (GNU/Linux)

iD8DBQBE4i6twPD5Cr/3CJgRAsa1AKCDi5jK7OCJ5CHOPDWILbVEPI//PQCgj/Lo
48iqhnjUfcSUDujbENkEuno=
=SQ93
-----END PGP SIGNATURE-----

--nextPart1349417.xmmVqDXbTi--
 
R

Robert Klemme

fabsy said:
Hey!
I have been trying to find a goto function in ruby..
Like this:

foo: bar
If blabla then GOTO foo

Is there any command for exiting the app?
And how do I search ex a textfile for a certain word and delete it?


------

if value == "2"
system('clear')
print "[Opening file..]\n\n"
r_file = File.open( "file", "a") #Wrote this so I wouldn't get an
error like "File doesn't exist".
r_file = File.open( "file", "r")
print "-----\n"
print r_file.read
print "-----"
print "\n"
system ('ruby note.rb') #is there anyway to restart or goto the
beginning of the script?
end

begin
....
end while your_condition_here

begin
....
end until your_condition_here

If you need an endless loop

loop do
....
end

See also
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#S6

Note that your script opens the file over and over again and never
closes it. This can cause problems - especially since you do it with
different modes.

Kind regards

robert
 
F

fabsy

begin
...
end while your_condition_here

begin
...
end until your_condition_here

If you need an endless loop

loop do
...
end

See also
http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#S6

Note that your script opens the file over and over again and never
closes it. This can cause problems - especially since you do it with
different modes.

Kind regards

robert

I don't really understand..

I want the script to start over again.. for example..
When the user made an input and pressed return I want the script to
"restart" or jump to the beginning..
 
L

Logan Capaldo

I don't really understand..

I want the script to start over again.. for example..
When the user made an input and pressed return I want the script to
"restart" or jump to the beginning..


I wouldn't recommend making this a habit but:

% cat cc.rb
x = 0
bar = callcc {|cc| cc}
puts "Hello"
x += 1
bar.call(bar) unless x == 3
puts "x is #{x}"

% ruby cc.rb
Hello
Hello
Hello
x is 3
 
G

gwtmp01

I wouldn't recommend making this a habit but:

% cat cc.rb
x = 0
bar = callcc {|cc| cc}
puts "Hello"
x += 1
bar.call(bar) unless x == 3
puts "x is #{x}"

Yikes, someone asks questions which indicate that they
are a novice programmer and you throw continuations at them?

Isn't that like throwing someone in the deep end of the pool and
seeing if they can swim? Or maybe it is like holding them under
water and seeing if they have gills? :)
I don't really understand..

I want the script to start over again.. for example..
When the user made an input and pressed return I want the script to
"restart" or jump to the beginning..

You can use 'next' to begin the next iteration of a loop:

loop {
# your code here
if startover?
next # will cause loop to start over
end
# more stuff here
if quit?
break # to get out of the loop entirely
end
# more stuff here
}


Gary Wright
 
D

David Vallner

I want the script to start over again.. for example..
When the user made an input and pressed return I want the script to
"restart" or jump to the beginning..

Let me introduce you to my good friend: structured programming. I'm sure=
=

you'll get along marvelous once you get to know each other.

The BASIC construct of "goto" is rightfully considered harmful for =

programs above a certain complexity. It's not present in Ruby in any =

direct way that would let you jump around a program's structure =

arbitrarily. (Actually, that's a lie, but I'll hold the continuations fo=
r =

the sake of simplicity.)

If you want a script to start over, the easiest way is to put all the =

logic of the script into a function / procedure / whatever, and repeated=
ly =

call that one in a loop like Darren and Robert indicated.

If you want the script to react to user input, you could for example do:=


def do_stuff_with_file(r_file)
# Most of your original code goes here.
end

should_print_file =3D true
while should_print_file

# Better way to avoid file not found errors - you're creating a complet=
ely
# useless temporary file as a workaround. =

File.open('file') { | r_file | do_stuff_with_file(r_file) } if =

File.exist? ('file')

puts('Print file again? [y/n]')
should_print_file =3D (gets[0] =3D=3D 'y') # Flaky bit, should be more =
=

forgiving.

end

David Vallner
 
T

Timothy Goddard

fabsy said:
Hey!
I have been trying to find a goto function in ruby..
Like this:

foo: bar
If blabla then GOTO foo

Is there any command for exiting the app?
And how do I search ex a textfile for a certain word and delete it?


------

if value == "2"
system('clear')
print "[Opening file..]\n\n"
r_file = File.open( "file", "a") #Wrote this so I wouldn't get an
error like "File doesn't exist".
r_file = File.open( "file", "r")
print "-----\n"
print r_file.read
print "-----"
print "\n"
system ('ruby note.rb') #is there anyway to restart or goto the
beginning of the script?
end

There is a reason why there are no gotos in Ruby or the vast majority
of modern languages. They are the complete antithesis of good program
design. You should be writing something more like this:

while value == 2
begin
system("clear") # This is a pretty bad idea - it's not portable and
completely unneccesary.
puts "[Opening file...]"
puts
File.open("file", "r") do |file|
puts "---"
puts file.read
puts "---"
end
system("ruby note.rb") # What is this for?
rescue Errno:ENOENT
puts "File does not exist."
end
end

This program properly handles file closing and uses exception handling
to pick up problems with opening the file. Exceptions and simple loops
can produce the same result as any GOTO mess while maintaining good
structure. I would avoid system() as well, but you may have your
reasons for using it.
 
N

Nathan Smith

Let me introduce you to my good friend: structured programming. I'm sure
you'll get along marvelous once you get to know each other.

The BASIC construct of "goto" is rightfully considered harmful for
programs above a certain complexity. It's not present in Ruby in any
direct way that would let you jump around a program's structure
arbitrarily. (Actually, that's a lie, but I'll hold the continuations for
the sake of simplicity.)

<snip>

Obligatory response:

In some very rare circumstances, using gotos does in fact have a rightful
place in code. In very long switch/case statements in C code, a _very_
well structured (and properly named) set of labels/gotos can make code
much cleaner, and easier to understand, than if they were not used. But
I'll say again, this is very rare.

Nate
 
F

fabsy

Timothy Goddard skrev:
fabsy said:
Hey!
I have been trying to find a goto function in ruby..
Like this:

foo: bar
If blabla then GOTO foo

Is there any command for exiting the app?
And how do I search ex a textfile for a certain word and delete it?


------

if value == "2"
system('clear')
print "[Opening file..]\n\n"
r_file = File.open( "file", "a") #Wrote this so I wouldn't get an
error like "File doesn't exist".
r_file = File.open( "file", "r")
print "-----\n"
print r_file.read
print "-----"
print "\n"
system ('ruby note.rb') #is there anyway to restart or goto the
beginning of the script?
end

There is a reason why there are no gotos in Ruby or the vast majority
of modern languages. They are the complete antithesis of good program
design. You should be writing something more like this:

while value == 2
begin
system("clear") # This is a pretty bad idea - it's not portable and
completely unneccesary.
puts "[Opening file...]"
puts
File.open("file", "r") do |file|
puts "---"
puts file.read
puts "---"
end
system("ruby note.rb") # What is this for?
rescue Errno:ENOENT
puts "File does not exist."
end
end

This program properly handles file closing and uses exception handling
to pick up problems with opening the file. Exceptions and simple loops
can produce the same result as any GOTO mess while maintaining good
structure. I would avoid system() as well, but you may have your
reasons for using it.

Naa, Im just testing to create something simple.. I think thats the
best way of learning..
So the system() thingy is just for test. :)
the system("ruby note.rb") was just a solution to get the script to
"restart", I know it's a bad way but I had to try it. The system(clear)
was just so the screen was cleared.
 
G

Gennady Bystritsky

=20
-----Original Message-----
From: Nathan Smith [mailto:[email protected]]=20
Sent: Tuesday, August 15, 2006 3:09 PM
To: ruby-talk ML
Subject: Re: goto function?
=20
On Wed, 16 Aug 2006, David Vallner wrote:
=20
=20
<snip>
=20
Obligatory response:
=20
In some very rare circumstances, using gotos does in fact=20
have a rightful
place in code. In very long switch/case statements in C code, a _very_
well structured (and properly named) set of labels/gotos can make code
much cleaner, and easier to understand, than if they were not=20
used. But
I'll say again, this is very rare.

I have to respectfully disagree. Having actively used C (among many
others) since 1986 up until now for professional development, I NEVER
found a good use for it, even in my early days. Especially in
switch/case statements ;-). It may be _very_ well structured in the
beginning, becoming a total mess with time. Never had very long
switch/case statements even on very big projects either. This is where
lookup tables with function pointers come in handy, for one.

Gennady.
 
E

Eero Saynatkari

fabsy said:
Hey!
I have been trying to find a goto function in ruby..
Like this:

foo: bar
If blabla then GOTO foo

Is there any command for exiting the app?
And how do I search ex a textfile for a certain word and delete it?

<snip for rforum />

Since no-one mentioned it, how about a throw/catch solution?
 
D

David Vallner

Since no-one mentioned it, how about a throw/catch solution?

Throw / catch is for hairier nonlocal returns, he wanted to repeat
something using goto. Looping with a break or an end condition is easier
to understand (YMMV) if you don't need to get a result value of any sort.
But yes, that would also work. Especially since you can implement any
control construct with ruby's throw / catch if you're stark raving mad
enough.

David Vallner
 
J

Jeremy Tregunna

Throw / catch is for hairier nonlocal returns, he wanted to repeat
something using goto. Looping with a break or an end condition is
easier to understand (YMMV) if you don't need to get a result value
of any sort. But yes, that would also work. Especially since you
can implement any control construct with ruby's throw / catch if
you're stark raving mad enough.

Even if you need a return value you can break with a value. I.e.,

i = 0
loop do
i = i + 1
break i * i if i == 5
end

will return 25.
David Vallner

--
Jeremy Tregunna
(e-mail address removed)


"One serious obstacle to the adoption of good programming languages
is the notion that everything has to be sacrificed for speed. In
computer languages as in life, speed kills." -- Mike Vanier
 
L

Logan Capaldo

Since no-one mentioned it, how about a throw/catch solution?
I originally tried to do it with throw/catch, but its not really
"goto-y" enough. Hence the continuation solution.
 
R

Robert Klemme

I don't really understand..

You call File.open() but you do not call #close(). Note that there are
other means to read from a file, e.g you can do File.readlines or
File.read. See RDOC here http://www.ruby-doc.org/core/
I want the script to start over again.. for example..
When the user made an input and pressed return I want the script to
"restart" or jump to the beginning..

That's why I gave you the looping constructs.

robert
 
F

fabsy

Yes, I fixed the file.close thing. :)
It was something I missed, the main problem I had was to get the script
to "start over".
Thanks everybody..
 
S

Simen Edvardsen

It is often said that continuations are as powerful as goto.
But is there a way to jump *forward* with continuations, without
rewriting the code to use additional methods?

A continuation is, by definition, the *continuation* of the
computation. But that probably wasn't the answer you wanted. There's
no way to do:

goto :a
label :a

Even though it's easy to create label and goto methods that work like this:

label :a
puts "after a"
goto :a if rand(2) % 2 == 0
 
L

Logan Capaldo

It is often said that continuations are as powerful as goto.
But is there a way to jump *forward* with continuations, without
rewriting the code to use additional methods?

Jumping forward:

callcc do |cc|
puts "Some code"
puts "Some more code"
cc.call # jump forward
puts "I'm never executed"
end
puts "Whee!"

 
D

David Vallner

It is often said that continuations are as powerful as goto.
But is there a way to jump *forward* with continuations, without
rewriting the code to use additional methods?

As others said, continuations continue. You can jump backwards, because
you know the whole context to jump into - it already existed.

To jump forward in code without explicitly setting up a context in which
to run would either result in undefined behaviour, accepting some defaults
for the context (all local / instance / other nonglobal variables are nil,
for example), or the timespace continuum breaking. That might not apply if
you were scripting a time machine, of course.

This is precisely why goto is considered harmful in the first place - to
jump forward, you have to use only global context, or some other context
that is defined to be shared when using such a construct. It would be
interesting however if there was a concept of capture a (possible) future
point of code execution without having to do explicit setup via a command
object or something similar. I'll admit at this point that my savvy gets a
little fuzzy around very high level programming theory - so I might
actually be honking wrong in which case I'm all ears intrigued as to the
workings of that construct.

David Vallner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top