Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Options menu in Ruby

Tags:

ruby

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.

like image 539
RossDoughty Avatar asked Mar 10 '11 09:03

RossDoughty


3 Answers

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
like image 156
Jonas Elfström Avatar answered Nov 12 '22 08:11

Jonas Elfström


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.

like image 22
DarkDust Avatar answered Nov 12 '22 08:11

DarkDust


I was about to give you just a few tips - but your code does quite a few things wrong:

  1. Indentation - make your code readable!
  2. Code in class bodies is valid in ruby, but that doesn't make it a good idea - it's main use is for metaprogrammming.
  3. Don't capture user input into constants. They're not.

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
like image 1
Jakub Hampl Avatar answered Nov 12 '22 08:11

Jakub Hampl