From: Robert Klemme Date: 2013-02-16T04:07:07+09:00 Subject: Re: so, what's the proper way to replace funcionality of GOTO ? On Fri, Feb 15, 2013 at 7:17 PM, Stu P. D'naim wrote: > Firstly, I know what are Functions/Methods, but I find GOTO much easier > to follow (and also more powrfull, because I can RETURN wherever I want > - in GOSUB, and Functions, you always return to the point you started > from) Where's the difference? With a GOSUB the RETURN will also take you to the calling site. That's the whole point of GOSUB vs. GOTO. What makes GOSUB worse than functions is that you do not get a new scope and do not have function arguments. So you always risk messing up the caller's scope. Granted, more modern dialects of BASIC might have mechanisms to deal with that (it's been a while I worked with BASIC - and I wouldn't got back unless for experimental purposes). > I had idea (as described in original post at #2) to write code something > like this: > ------------------------------------------------ > class Mymenus > > attr_acessor :over100variablesIintendtoUse > > def initialize > #initialize every variable > end > > def menu_00 #root menu > system("cls") > puts "Select something:" > puts > puts "1 - open menu_1" > puts "2 - open menu_2" > puts "3 - open menu_3" > puts "4 - open menu_4" > puts "q - Quit" > end > > def menu_1 > system("cls") > puts "enter choice:" > puts > puts "1 - open menu_11" > puts "2 - open menu_12" > puts "3 - open menu_13" > puts "4 - open menu_14" > puts "b - Back" > end > > def menu_2 > system("cls") > puts "enter choice:" > puts > puts "1 - open menu_21" > puts "2 - open menu_22" > puts "b - Back" > end > > ... etc ... etc > --------------------------------------------------------- > ... and then probably put loop, starting with root menu, when you select > number, you call new method (submenu) ... but your way seems easier, and > with less methods involved I do not know what your menus will do eventually but I would certainly make this more data driven. In this example the structure of the code is identical for all menu entries. There is no if else cascade or case statement. The proper item is just determined via the position in the Array: Menu = Struct.new :name, :code # convenience method to create a Menu instance def menu(name, &b) Menu.new name, b end menus = [ menu("Configuration") do print "Starting configuration..." sleep 5 puts " done." end , menu("Reset everything") do puts "back to square one" end, menu("Quit") do puts "quitting" exit 0 end, ] loop do puts "Enter your choice:" menus.each_with_index do |m, idx| printf "%2d %s\n", idx, m.name end print "> " input = gets.chomp begin m = menus[Integer(input)] if m m.code.call else puts "Wrong choice. Try again" end rescue ArgumentError puts "Not numeric: #{input}" end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/