I am trying to create a menu in Ruby so that depending on what a user inputs, depends on what class is called. Then afterwords it will return to the "Main" or the class "Options" in this case.
I 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 also would like to be able to after the class has been called to return to the clas Options. I'm sure this is easy, but I can't work it out.
Thankyou again,
Ross.
You could check out Highline. It's a nice and simple framework for creating console applications. Install it with
sudo gem install --no-rdoc --no-ri highline
Here's 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
While I don't really get what is wanted here, one thing that immediately jumped to my eye is this 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
gets
returns a string. So you should do:
case gets().strip()
when "1"
puts "AccelCalc"
when "2"
puts "EnergyCalc"
else
puts "Invalid input."
end
I've used explicit brackets here, instead of gets().strip()
you can simply write gets.strip
in Ruby. What this expression does is read something from the standard input and remove all whitespace around it (newline from pressing the enter key). Then the resulting string is compared.
I was about to give you just a few tips - but your code does quite a few things wrong:
So how would I code it? Something like this:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With