Skip to content

Instantly share code, notes, and snippets.

@danielpatricio
Last active March 27, 2016 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpatricio/e08700ffbd23b493e5fe to your computer and use it in GitHub Desktop.
Save danielpatricio/e08700ffbd23b493e5fe to your computer and use it in GitHub Desktop.
#This is being returned from the API
request =
{"orders"=> [
{"total_price"=>"31.46"},
{"total_price"=>"34.95"},
{"total_price"=>"31.46"},
{"total_price"=>"50.95"},
{"total_price"=>"39.57"},
{"total_price"=>"112.37"}
]}
# This script outputs each price but I need to add them to array so I can add them with inject
orders = request["orders"].each do |order|
price = order["total_price"]
puts price
end
#Todo - Add all the order values to calculate a total
orders.inject(0){ | total, order | total + order }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment