From: Tim Romberg Date: 2010-09-07T04:38:39+09:00 Subject: Re: A better idiomatic way of doing this?! Brian Candler wrote: > 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. @Brian: Thanks Brian you were right. I got that to role my only problem is now that it currently displays both menus at the same time. I guess thats logical as its calling both menu objects at the same time. i only want the main menu to show when you start it, the other menus should function as submenus. Im actually having my main in a seperate file and it looks like this: require 'menu_test' =begin Main class for the program. Creates a new camping and starts the loop fpr the program =end class Main if __FILE__ == $0 $camping = Camping.new(32, 12) # creates new camping include Menus $current_menu = main_menu # loops through menu while (true) puts $current_menu choice = Menus.get_input $current_menu.make_menu_choice(choice) end end end Buff..I fel like Im making things more complicated but Im laking the proper design pattern skills for Ruby. I like what you said about: module Menus class Menu .. end end m = Menus::Menu.new and to use that as a main instead. Big thanks and regards Tim -- Posted via http://www.ruby-forum.com/.