Ruby中的选项菜单

时间:2011-03-10 09:48:48

标签: ruby

我正在尝试在Ruby中创建一个菜单,以便根据用户输入的内容,取决于调用的类。然后在这种情况下,它将返回“Main”或“Options”类。

我希望有人可以帮助我。这是我的代码。

module Physics
G = 21
C = 20000
Pi = 3.14
D = 100
end

class Options
puts "Please select 1 for Acceleration and 2 for Energy."
option = gets()
if option == 1
then
puts "AccelCalc" # This is the bit that needs to direct the user to the class AccelCalc
else
puts "EnergyCalc" # This needs to take them to EnergyCalc.
end
end

class AccelCalc
include Physics
puts "Please enter the mass of the object"
M = gets()
puts "The mass of the object is " + M + "."
puts "Please enter the speed of the defined object."
S = gets()
puts "The speed of the object is set at " + S + "."
puts "The acceleration will now be calculated."
puts S.to_i*M.to_i
end

class EnergyCalc
include Physics
puts "This will use the function E=MC^2 to calculate the Energy."
puts "Enter object mass"
M = gets()
puts "The mass is " + M + "."
puts "Now calculating the Energy of the object."
puts M.to_i*C_to.i**2
end
$end

我也希望能够在类被调用后返回clas选项。我确信这很容易,但我无法解决。

再次谢谢你,

罗斯。

3 个答案:

答案 0 :(得分:12)

您可以查看Highline。这是一个用于创建控制台应用程序的简单框架。用

安装
  

sudo gem install --no-rdoc --no-ri highline

这是一个例子。

require "rubygems"
require "highline/import"

@C = 299792458

def accel_calc
  mass = ask("Mass? ", Float)
  speed = ask("Speed? ", Float)
  puts
  puts("mass * speed = #{mass*speed}")
  puts
end

def energy_calc
  mass = ask("Mass? ", Float)
  puts
  puts("E=MC^2 gives #{mass*@C**2}")
  puts
end

begin
  puts
  loop do
    choose do |menu|
      menu.prompt = "Please select calculation "
      menu.choice(:Acceleration) { accel_calc() }
      menu.choice(:Energy) { energy_calc() }
      menu.choice(:Quit, "Exit program.") { exit }
    end
  end
end

答案 1 :(得分:1)

虽然我并没有真正得到这里想要的东西,但立即跳到我眼前的一件事是这个块:

option = gets()
if option == 1
then
puts "AccelCalc" # This is the bit that needs to direct the user to the class AccelCalc
else
puts "EnergyCalc" # This needs to take them to EnergyCalc.
end

gets返回一个字符串。所以你应该这样做:

case gets().strip()
when "1"
  puts "AccelCalc"
when "2"
  puts "EnergyCalc"
else
  puts "Invalid input."
end

我在这里使用了明确的括号,而不是gets().strip(),你可以简单地在Ruby中编写gets.strip。这个表达式的作用是从标准输入读取内容并删除它周围的所有空格(按下回车键的换行符)。然后比较结果字符串。

答案 2 :(得分:1)

我准备给你一些提示 - 但是你的代码确实存在很多错误:

  1. 缩进 - 让你的代码可读!
  2. 类体中的代码在ruby中有效,但这并不是一个好主意 - 它的主要用途是元编程。
  3. 不要将用户输入捕获到常量中。他们不是。
  4. 那我该怎么编码呢?像这样:

    class App
    
      def initialize
        main_menu
      end
    
      def navigate_to(what)
        what.new.display
        main_menu
      end
    
      def main_menu
        puts "Please select 1 for Acceleration and 2 for Energy."
        case gets.strip
        when "1"
          navigate_to AccelCalc
        when "2"
          navigate_to EnergyCalc
        else
          puts "Choose either 1 or 2."
          main_menu
        end
      end
    end
    
    class AccelCalc
      include Physics
      def display
        puts "Please enter the mass of the object"
        @m = gets.to_f
        puts "The mass of the object is #{@m}."
        puts "Please enter the speed of the defined object."
        @s = gets.to_f
        puts "The speed of the object is set at #{@s}."
        puts "The acceleration will now be calculated."
        puts @s * @m
      end
    end
    
    # ...
    
    App.new    # Run teh codez