Ruby Options Menu

I am trying to create a menu in Ruby so that depending on what the user enters, it depends on which class is being called. Then the afterword he will return to the “Main” or to the “Parameters” class in this case.

Hope someone can help me. Here is my code.

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

I would also like to be able to return to the class parameters after calling the class. I am sure it is easy, but I cannot solve it.

Thanks again,

Ross

+5
source share
3 answers

You can check out Highline . This is a good and simple structure for creating console applications. Install it with

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

Here is an example.

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
+10
source

, :

  • - !
  • , - .
  • . .

, ? - :

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
+1

While I really don't get what I need here, one thing that immediately jumped in my opinion is the block:

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

getsreturns a string. So you should do:

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

Here I used explicit brackets, instead gets().strip()you can just write gets.stripin Ruby. What this expression does is read something from standard input and removes all spaces around it (newline from pressing the enter key). Then the resulting string is compared.

0
source

All Articles