From: Tim Romberg Date: 2010-09-07T01:59:53+09:00 Subject: Re: A better idiomatic way of doing this?! Brian Candler wrote: > On Mon, Sep 06, 2010 at 06:12:39AM +0900, Tim Romberg wrote: >> module Menus >> >> class Main_Menu >> # This is a class method because of the "self" >> def self.main_menu >> puts "---------------------------" > > Is the issue that you want to be able to create multiple menus from the > same > code? Then make it data-driven. > > Your current code never creates any instances of Main_Menu, but instead > defines methods on the class itself (e.g. def self.main_menu). If you > define > instance methods, then each instance can have its own data structure. > e.g. > > class Menu > # Pass in array of options > def initialize(options) > @options = options > end > > def main_menu > puts "---------------------------" > puts " Menu" > @options.each_with_index do |item, i| > puts " #{i+1}. #{item}" > end > puts > puts "What do you want to do?" > #... etc > end > end > > m = Menu.new ["Checkin","Checkout","Lists","Economy","Exit"] > m.main_menu > > Continue to make the rest of your code data-driven. e.g. Instead of > >> while input > 5 || input < 1 do > > you can write > >> while input > @options.size || input < 1 do > > I suggest that make_choice should move out of this class, and be the > responsibility of the caller to do something based on the returned > value. > (If you really wanted to integrate this you could pass in an array of > lambdas for code to be executed on each choice, but I don't really see > the > need for this added complexity) > >> class Main_Menu >> include Menus > > I've no idea why you're including Menus into the class - especially as > at > the moment you're not creating any instances of the class. I'd drop > that. > > Regards, > > Brian. @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. 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 etc. Thanks for the help! -- Posted via http://www.ruby-forum.com/.