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.

@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:

module Menus
  def self.getValidNumber
    input = gets.chomp

    while input > @options.size || input < 1 do
      puts "cant do that tru again."
      input = gets.chomp
    end

    number = input.to_f
    if (number <= 0)
      puts "cant state a negative value."
      getValidPositiveNumber
    end
    return number
  end


  def self.get_valid_input(options)

    input = gets.chomp

    while (!options.include?(input) && !options.include?(input.to_i))
      puts "No good, you have to choose a value between " + 
valid_options.inspect
      input = gets.chomp
    end
    return input

  end


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

   def self.make_choice(choice)
       # chooses something from the menu based on the choice
        case choice
          when 1:
             check_in
          when 2:
             check_out
          when 3:
             puts $in_menu = lists_menu
          when 4:
             puts $in_menu = economy_menu
          when 5:
             puts "You are now leaving the camping, welcome back!"
             exit
           end
         end
       end

   def lists_menu
     puts "---------------------------"
     puts "      List Menu"
     @options.each_with_index do |item, i|
        puts "  #{i+1}. #{item}"
      end
      puts
      puts "What do you want to do?"
    end

    def self.make_choice(choice)
      case choice
        when 1
          puts $camping
        when 2
          puts $camping.history.all_guests
        when 0
          $in_menu = main.menu
      end
    end

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

end

I get an undefined method lists_menu' for #<Menus::Menu:0x10019aa90> 
(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

Regards
Tim


-- 
Posted via http://www.ruby-forum.com/.