0

I am working on a Ruby project to make a road trip calculator. The program takes the miles per gallon and fuel tank size from multiple users to calculate and compare fuel economy. In the following portion of code I want to iterate over an array named vehicles and divide 3000 by the mpg of each element in that array.

I'm struggling with what I want to do next: take those outputs and sum them (3000 / total_fuel[:mpg] + 3000 / total_fuel[:mpg] + 3000 / total_fuel[:mpg] ... of each element). So far this is what I have:

    vehicles.each do |total_fuel|
      total_fuel = (3000 / total_fuel[:mpg])
      print total_fuel
    end

When it prints it just outputs the calculations right next to each other with no spaces (eg 150166). I'm totally lost on how to then take those numbers and add them, especially since it will be a different amount of outputs depending on how many users entered info.

I hope that was clear. Any ideas?

KarGuad
  • 11
  • 2
  • Can you provide a sample of the array with few elements? Or maybe `vehicles.sum { |total_fuel| 3000.0 / total_fuel[:mpg] }`? – iGian Oct 25 '18 at 09:34
  • @iGian here is an example of the array with three inputs `[{:name=>"John", :mpg=>24, :tank=>15, :maintenance=>4}, {:name=>"Sue", :mpg=>16, :tank=>10, :maintenance=>2}, {:name=>"Tom", :mpg=>31, :tank=>20, :maintenance=>5}]
    ` The line of code you provided worked though!
    – KarGuad Oct 25 '18 at 09:53
  • Please put your sample input in the question itself. Also, how are you comparing fuel economy if you are summing **across** the vehicles? – pjs Oct 25 '18 at 11:25
  • Possible duplicate of [How to sum array of numbers in Ruby?](https://stackoverflow.com/questions/1538789/how-to-sum-array-of-numbers-in-ruby) – SRack Oct 25 '18 at 11:47

3 Answers3

3

It sounds like what you need is the sum method. In your case, you don't want to just iterate over all vehicles and execute a block, but you want to sum the results of the block and return a single value.

 total_fuel = vehicles.sum do |vehicle|
   3000 / vehicle[:mpg]
 end

A closely related and very useful method is reduce, which allows you to specify how exactly multiple values should be reduced into one. Here is an example that does the same as sum:

# Sum some numbers
(5..10).reduce(:+)                             #=> 45

But since you can specify exactly how the values should be combined, the method can be applied in more cases than sum.

Check out the documentation for the Enumerable module for more helpful methods that work on arrays and hashes.

jdno
  • 4,104
  • 1
  • 22
  • 27
0

You should use map and sum

total_fuel = vehicles.map { |vehicle| 3000 / vehicle[:mpg] }.sum

or

total_fuel = vehicles.map { |vehicle| 3000 / vehicle[:mpg] }.reduce(:+)
# for Ruby version < 2.4
Mihail Panayotov
  • 320
  • 2
  • 11
0

Just another option using Enumerable#inject:

total_fuel =  = vehicles.inject(0) { |total_fuel, vehicle| total_fuel += 3000.0 / vehicle[:mpg] }

Please, use 3000.0 instead of 3000, so you get float result.

iGian
  • 11,023
  • 3
  • 21
  • 36