From: Tim Romberg Date: 2010-09-07T04:08:27+09:00 Subject: Re: A better idiomatic way of doing this?! Adam Prescott wrote: > On Mon, Sep 6, 2010 at 6:23 PM, Brian Candler > wrote: >> >> On Tue, Sep 07, 2010 at 01:59:53AM +0900, Tim Romberg wrote: >> > @Brian: Thanks for your feedback. I am aware that I'm not practicing >> > "good" Ruby yet, I hope with more time I will develop. The reason why >> > included the module was because one of the musts with the assignment was >> > to output a menu through a module. >> >> In your original code you only had class methods, so you could have defined >> them directly on a module instead: >> >> module Menu >>  def self.whatever >>    puts "Hello" >>  end >> end >> Menu.whatever >> >> This is because a class *is* a module, just with the extra abilities of >> creating instances and subclassing, which you weren't using. >> >> > When I want to create the other menus >> > as you stated with the main_menu example can I just continue creating >> > the menu objects equally?! For example: >> > m = Menu.new ["Checkin","Checkout","Lists","Economy","Exit"] >> > m.main_menu >> > m = Menu.new ["list current guests","list all guests","back to main >> > menu"] >> >  m.lists_menu >> >> Sure; or store them in different variables (or constants) and then re-use >> them as you like.  You might choose a more descriptive name for your method >> which prints the menu and lets the user choose a selection. >> >> main_menu = Menu.new ["Checkin","Checkout","Lists","Economy","Exit"] >> lists_menu = Menu.new ["list current guests","list all guests","back to main >> menu"] >> main_menu.choose >> lists_menu.choose >> >> Regards, >> >> Brian. >> > > I'd also like to add that this would be good for simplicity: > > class Menu > def self.[](*args) > self.new(*args) > end > end > > to allow you to say > > main_menu = Menu["Checkin","Checkout","Lists","Economy","Exit"] @Adam: How exactly would you incorporate that?! Now the class menu is as follows: class Menu attr_reader :options # Pass in array of options def initialize(options) @options = options end def main_menu puts "---------------------------" puts " Main Menu" @options.each_with_index do |item, i| puts " #{i+1}. #{item}" end puts puts "What do you want to do?" end Regards Tim -- Posted via http://www.ruby-forum.com/.