使用Ruby在用户输入的模块中输出菜单?

时间:2010-08-28 14:41:50

标签: ruby

什么是使用Rubys模块输出带有选项的菜单的最好或者只是一个好方法?! 现在我这样做,并且运作良好。

    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   

但是我希望能够在这个模块中再增加2个菜单,但它应该首先显示这个主菜单。然后,当您选择列表时,您将进入列表菜单,当您选择经济时,您应该进入经济菜单。有什么好建议吗?!

由于

感谢您的解决方案。但是如何将其与模块相结合?!我在想这样的事情:

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.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 

2 个答案:

答案 0 :(得分:1)

尝试highline gem。

答案 1 :(得分:0)

如果您要编写自己的代码,可能需要以编程方式执行。对所有菜单选项进行硬编码是不可行的。我感兴趣(并且很无聊),所以我把它编码了。

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