Beginner Needs Help! Create Array of Hash Objects


#1

Hello! I am currently attempting to create a program that compares vehicle efficiencies for a road trip. My first draft worked but looking over it, I don’t think I could’ve created a longer amount of code for what I needed to do. I know there is a simpler/better way.

I’m trying to rewrite it and have the user inputs stored in an array. I’m having a lot of trouble figuring out what I’m doing incorrectly. All advice is gratefully received!

1st Draft:
distance = 3000.00
gallon_price = 3.00
#Welcome statement
puts “Hello, this program will assist you in planning for your roadtrip from Seattle to Boston!”
puts “”

#Vehicle 1 user inputs
#User inputs converted to float value
puts “Please enter the following information for your 1st vehicle.”
puts “What is the average mileage per gallon?”
veh1_milespergallon = gets.chomp.to_f
puts “What is the gas tank capacity?”
veh1_gastankvol = gets.chomp.to_f
“What is the vehicle maintenance cost at each refueling stop?”
veh1_maintcost = gets.chomp.to_f

#Vehicle 2 user inputs
puts “Please enter the following information for your 2nd vehicle.”
puts “What is the average mileage per gallon?”
veh2_milespergallon = gets.chomp.to_f
puts “What is the gas tank capacity?”
veh2_gastankvol = gets.chomp.to_f
puts “What is the vehicle maintenance cost at each refueling stop?”
veh2_maintcost = gets.chomp.to_f

#Vehicle 3 user inputs
puts “Please enter the following information for your 3rd vehicle.”
puts “What is the average mileage per gallon?”
veh3_milespergallon = gets.chomp.to_f
puts “What is the gas tank capacity?”
veh3_gastankvol = gets.chomp.to_f
puts “What is the vehicle maintenance cost at each refueling stop?”
veh3_maintcost = gets.chomp.to_f

#Vehicle 4 user inputs
puts “Please enter the following information for your 4th vehicle.”
puts “What is the average mileage per gallon?”
veh4_milespergallon = gets.chomp.to_f
puts “What is the gas tank capacity?”
veh4_gastankvol = gets.chomp.to_f
puts “What is the vehicle maintenance cost at each refueling stop?”
veh4_maintcost = gets.chomp.to_f

#Set variables for program outputs

#Vehicle 1 variables
veh1_distancepertank = veh1_milespergallon * veh1_gastankvol
veh1_refuelstops = distance / veh1_distancepertank
veh1_totaltripcost = ((veh1_refuelstops * veh1_maintcost) + (gallon_price * (distance / veh1_milespergallon)))

#Vehicle 2 variables
veh2_distancepertank = veh2_milespergallon * veh2_gastankvol
veh2_refuelstops = distance / veh2_distancepertank
veh2_totaltripcost = ((veh2_refuelstops * veh2_maintcost) + (gallon_price * (distance / veh2_milespergallon)))

#Vehicle 3 variables
veh3_distancepertank = veh3_milespergallon * veh3_gastankvol
veh3_refuelstops = distance / veh3_distancepertank
veh3_totaltripcost = ((veh3_refuelstops * veh3_maintcost) + (gallon_price * (distance / veh3_milespergallon)))

#Vehicle 4 variables
veh4_distancepertank = veh4_milespergallon * veh4_gastankvol
veh4_refuelstops = distance / veh4_distancepertank
veh4_totaltripcost = ((veh4_refuelstops * veh4_maintcost) + (gallon_price * (distance / veh4_milespergallon)))

puts “Thank you!”
puts “The information you provided was used to calculate the following for each vehicle.”

#Vehicle 1 outputs
puts “”
puts “Vehicle 1:”
puts “Distance per gas tank = #{veh1_distancepertank.round(3)} miles.”
puts “Number of refueling stops required = #{veh1_refuelstops.ceil} stops.”
puts “Total cost of trip = $#{veh1_totaltripcost.round(2)}.”

#Vehicle 2 outputs
puts “”
puts “Vehicle 2:”
puts &“Distance per gas tank = #{veh2_distancepertank.round(3)} miles.”
puts “Number of refueling stops required = #{veh2_refuelstops.ceil} stops.”
puts “Total cost of trip = $#{veh2_totaltripcost.round(2)}.”

#Vehicle 3 outputs
puts “”
puts “Vehicle 3:”
puts “Distance per gas tank = #{veh3_distancepertank.round(3)} miles.”
puts “Number of refueling stops required = #{veh3_refuelstops.ceil} stops.”
puts “Total cost of trip = $#{veh3_totaltripcost.round(2)}.”

#Vehicle 4 outputs
puts “”
puts “Vehicle 4:”
puts “Distance per gas tank = #{veh4_distancepertank.round(3)} miles.”
puts “Number of refueling stops required = #{veh4_refuelstops.ceil} stops.”
puts “Total cost of trip = $#{veh4_totaltripcost.round(2)}.”

#Calculate total fuel burned if all 4 vehicles complete roadtrip
veh1_gallonsused = distance / veh1_milespergallon
veh2_gallonsused = distance / veh2_milespergallon
veh3_gallonsused = distance / veh3_milespergallon
veh4_gallonsused = distance / veh4_milespergallon
totalgallonsused = veh1_gallonsused + veh2_gallonsused + veh3_gallonsused + veh4_gallonsused

puts “”
puts “If all vehicles complete the roadtrip from Seattle to Boston, #{totalgallonsused.round(3)} gallons of fuel will be used.”

#Carpool comparison
a = veh1_gallonsused
b = veh2_gallonsused
c = veh3_gallonsused
d = veh4_gallonsused
totalcost = veh1_totaltripcost + veh2_totaltripcost + veh3_totaltripcost + veh4_totaltripcost

veh1_saves = totalcost - veh1_totaltripcost
veh2_saves = totalcost - veh2_totaltripcost
veh3_saves = totalcost - veh3_totaltripcost
veh4_saves = totalcost - veh4_totaltripcost

puts “”
if a < b and a < c and a < d

puts “If you choose to carpool, Vehicle 1 is the most fuel efficient choice. By carpooling in Vehicle 1, as opposed to driving separately, you will save $#{veh1_saves.round(2)}.”

elsif b < a and b < c and b < d

puts “If you choose to carpool, Vehicle 2 is the most fuel efficient choice. By carpooling in Vehicle 2, as opposed to driving separately, you will save $#{veh2_saves.round(2)}.”

elsif c < a and c < b and c < d

puts “If you choose to carpool, Vehicle 3 is the most fuel efficient choice. By carpooling in Vehicle 3, as opposed to driving separately, you will save $#{veh3_saves.round(2)}.”

elsif d < a and d < b and d < c

puts “If you choose to carpool, Vehicle 4 is the most fuel efficient choice. By carpooling in Vehicle 4, as opposed to driving separately, you will save $#{veh4_saves.round(2)}.”;

end

2nd draft (where I’m at so far)

#array

vehicles = []

#hash

veh_data = {}

#variables

distance = 3000

gallon_price = 3.00

#welcome statement

puts “Hello, this program will assist you in planning for your roadtrip from Seattle to Boston!”

puts “”

puts “How many vehicles would you like to evaluate today? Please enter a numeral.”

veh_count = gets.chomp.to_i

puts “Please enter the following information for your vehicle:”

puts “Make and Model?”

veh_type = gets.chomp

puts “Average MPG?”

mpg = gets.chomp.to_f

puts “Gas tank capacity? (in gallons)”

tank_vol = gets.chomp.to_f

puts “Maintenance cost at each refueling stop?”

maint_cost = gets.chomp.to_f

#calculations based on user inputs

tank_dist = mpg * tank_vol

fuel_stops = distance / tank_dist

fuel_cost_total = ((fuel_stops * maint_cost) + (gallon_price * (distance / mpg)))

veh_data < [veh_type, mpg, tank_vol, maint_cost, tank_dist, fuel_stops, fuel_cost_total]

veh_data.each do |veh_type, mpg, tank_vol, maint_cost, tank_dist, fuel_stops, fuel_cost_total|

puts “For the road trip from Seattle to Boston the veh_type has a tank distance of #{tank_dist}, will require approximately #{fuel_stops} fuel stops along the way, and the total fuel required will cost approximately $#{fuel_cost_total.round(2)}.”

end


#2

You may want to break your problem into smaller questions. Also, I would recommend storing your input data in a YAML formatted file. Use a text editor to enter data into file. Then you can slurp it into Ruby for processing.

require 'yaml'
db = YAML.load(File.read('trip_data.yaml'))

https://rhnh.net/2011/01/31/yaml-tutorial/


#3

I think you need to get used to using hashes. For example:

veh_data = {
  "veh_type" => nil,
  "etc"      => nil,
}
File.open('vehicle_data_starter_file.yaml', 'w') {|f| YAML.dump(veh_data)}

#4

Oh, I see what you’re asking now. This is what you do.

veh_data = {}
veh_data["mpg"] = 35.0
veh_data["tank_vol"] = 13.0
etc...
vehicles << veh_data

veh_data = {}
veh_data["mpg"] = 35.0
veh_data["tank_vol"] = 13.0
vehicles << veh_data

puts "Vehicle 1, mpg: #{vehicles[0]["mpg"]"

Of course, you would do this in a loop. But I still recommend you store your persistent data in YAML. I know it appears that the second “resetting” of veh_data to {} will destroy your previous data, but it won’t. Ruby maintains a pointer count that protects the data as long as something is still referring to it. In this case, there’s a pointer to the data stored in the vehicles Array, which is still in scope.