From: fabsy Date: 2006-08-16T07:15:09+09:00 Subject: Re: goto function? Timothy Goddard skrev: > fabsy wrote: > > 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 > > > > ----- > > > > Thanks! > > 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.