开发者

Outputting a menu in a module with user input using Ruby?

开发者 https://www.devze.com 2023-01-14 12:42 出处:网络
Whats the best or just a good way to output a menu with options with the use Rubys module?! Right now Im doing like this and its working well.

Whats the best or just a good way to output a menu with options with the use Rubys module?! Right now Im doing like this and its working well.

    MAIN_MENU = <<END 
"---------------------------" 
  Welcome to Ruby Camping!
      Menu
1. Checkin
2. Checkout
3. Lists
4. Economy
5. Exit

What do you want to do?
"---------------------------"
END 

end             

puts Menus::MAIN_MENU   

But I would like to be able to have 2 more menus in this module but it should only first show this one which is the main one. Then when you choose lists you get to the lists menu, and when you choose economy you should get to the economy menu. Any good suggestions?!

Thanks

Thanks for that solution. But how can I incorporate that with module?! I was thinking something like this:

module Menus

def self.getValidPositiveNumber
    input = gets.chomp

    while (input.to_i.to_s != input && input.to_f.to_s != input) do 
        puts "Ogiltig data. Försök igen."
        input = gets.chomp
    end

    number = input开发者_如何学Python.to_f
    if (number <= 0)
        puts "Du kan inte ange negativt värde."
        getValidPositiveNumber
    end
    return number
end

def self.get_valid_input(valid_options)

    input = gets.chomp

    while (!valid_options.include?(input) && !valid_options.include?(input.to_i))   

        puts "Ogiltigt värde. Skriv in ett nytt alternativ mellan " + valid_options.inspect
        input = gets.chomp
    end
    return input

end


class Menu

    attr_reader  :valid_options_range, :menu_string

    def initialize(valid_options_range, menu_string)
        @valid_options_range = valid_options_range
        @menu_string = menu_string
    end


    def do_menu_action(action)
        raise "Måste anropas i någon subklass!"
    end

    def to_s
        return @menu_string
    end
end

  MAIN_MENU = <<END 
 "---------------------------" 
  Welcome to Ruby Camping!
     Menu
 1. Checkin
 2. Checkout
 3. Lists
 4. Economy
 5. Exit

 What do you want to do?
"---------------------------"
END

print ": "

def make_menu_choice(choice)

case choice
    when 1:
      $camping.check_in
    when 2:
       $camping.check_out
    when 3:
      $current_menu = LISTS_MENU
    when 4:
      $current_menu = ECONOMY_MENU
    when 5:
      exit
   end
end


  LISTS_MENU = <<END
"---------------------------"   
-- 1. List current guests --
-- 2. List all guests --
--                          --
-- 0. Back to Main menu      --
------------------------------"
END


 def make_menu_choice(choice)
  case choice
    when 1:
      $camping
    when 2:
      $camping.all_guests
    when 0:
      $current_menu = MAIN_MENU
   end
end

 ECONOMY_MENU = <<END
 "---------------------------"   
 -- 1. List current guests --
 -- 2. List all guests --
 --                          --
 -- 0. Back to Main menu      --
 ------------------------------"
END


end 

puts Menus::MAIN_MENU
puts Menus::LISTS_MENU
puts Menus::ECONOMY_MENU 


Try the highline gem.


If you're going to code your own, you might want to do it programmatically. Hard-coding all the menu options in there isn't going to be very workable. I was interested (and bored), so I coded this up.

class Menu
  def menu_options
    self.class.instance_methods(false) - ['title']
  end

  def query
    puts title
    puts '=' * title.length

    menu_options.each_with_index do|meth,idx|
      puts " %3s: %s" % [
        idx + 1,
        meth.capitalize.gsub(/_(\w)/){ ' '+$1.upcase }
      ]
    end

    print '? '
    choice = gets.chomp.to_i - 1

    if choice >= menu_options.length or choice < 0
      puts "Invalid choice"
    end

    send(menu_options[choice])
  end

end

class MyMenu < Menu
  def title
    "My Awesome Menu"
  end

  def eat_cheese
    puts "I like cheese!"
  end

  def go_outside
    puts "Ahh, fresh air"
  end

  def quit_this_dumb_program
    :done
  end

end

menu = MyMenu.new
while menu.query != :done
end
0

精彩评论

暂无评论...
验证码 换一张
取 消