From: Brian Candler Date: 2010-09-07T04:05:33+09:00 Subject: Re: A better idiomatic way of doing this?! --pf9I7BMVVzbSWLtt Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Sep 07, 2010 at 03:12:30AM +0900, Tim Romberg wrote: > @Brian: you really are a gem. Thanks for all your help. I tried to > follow what you said put found it hard to incorporate all of it. I did > like this but does not seem to work so well: ... > I get an undefined method lists_menu' for # > (NoMethodError) > I guess I have declared something wrong somewhere. Do not mean to make > you debug all of my code but could you maybe give a hint?! Thanks If you reformat your code properly - see attached - it should become clearer. You have your "end"s misplaced, so you're defining lists_menu outside of the class, but inside the module. You might be able to find a good editor helps you with that, or there are utilities around for reformatting ruby source I think. If you have a copy of ruby 1.9 lying around, then running it with the -w flag will also give you hints where things aren't aligned properly (I keep 1.9 around for only that purpose) It would also be conventional to put your 'main program', the bit which creates the menus and runs them, outside of the enclosing module. That would give you: module Menus class Menu .. end end m = Menus::Menu.new It's a bit more unwieldy because of the use of the enclosing Module, which you have to use explicitly if you're outside. Regards, Brian. --pf9I7BMVVzbSWLtt Content-Type: application/x-ruby Content-Disposition: attachment; filename="menu.rb" Content-Transfer-Encoding: quoted-printable module Menus=0A def self.getValidNumber=0A input =3D gets.chomp=0A=0A = while input > @options.size || input < 1 do=0A puts "cant do that tr= u again."=0A input =3D gets.chomp=0A end=0A=0A number =3D input.= to_f=0A if (number <=3D 0)=0A puts "cant state a negative value."= =0A getValidPositiveNumber=0A end=0A return number=0A end=0A=0A= =0A def self.get_valid_input(options)=0A=0A input =3D gets.chomp=0A=0A = while (!options.include?(input) && !options.include?(input.to_i))=0A = puts "No good, you have to choose a value between " + valid_options.insp= ect=0A input =3D gets.chomp=0A end=0A return input=0A=0A end=0A= =0A=0A class Menu=0A=0A attr_reader :options=0A=0A # Pass in array o= f options=0A def initialize(options)=0A @options =3D options=0A = end=0A=0A def main_menu=0A puts "---------------------------"=0A = puts " Main Menu"=0A @options.each_with_index do |item, i|=0A = puts " #{i+1}. #{item}"=0A end=0A puts=0A puts "What= do you want to do?"=0A end=0A=0A def self.make_choice(choice)=0A = # chooses something from the menu based on the choice=0A case choice= =0A when 1=0A check_in=0A when 2=0A check_out=0A = when 3=0A puts $in_menu =3D lists_menu=0A when 4=0A p= uts $in_menu =3D economy_menu=0A when 5=0A puts "You are now le= aving the camping, welcome back!"=0A exit=0A end=0A end=0A = end=0A=0A def lists_menu=0A puts "---------------------------"=0A pu= ts " List Menu"=0A @options.each_with_index do |item, i|=0A pu= ts " #{i+1}. #{item}"=0A end=0A puts=0A puts "What do you want to= do?"=0A end=0A=0A def self.make_choice(choice)=0A case choice=0A w= hen 1=0A puts $camping=0A when 2=0A puts $camping.history.all_= guests=0A when 0=0A $in_menu =3D main.menu=0A end=0A end=0A=0A = m =3D Menu.new ["Checkin", "Checkout","Lists","Economy","Exit"]=0A m.main= _menu=0A m =3D Menu.new ["List current guests","List all guests","back to = main menu"]=0A m.lists_menu=0A=0Aend=0A --pf9I7BMVVzbSWLtt--