Summing a set of values from an API response but getting confused
flipping between hashes and arrays. This feels like it should be really
simple.
All code provided below
Summing a set of values from an API response but getting confused
flipping between hashes and arrays. This feels like it should be really
simple.
All code provided below
sum = request[“orders”].reduce(0) do |sum,item|
sum + item[“total_price”].to_r
end
puts sum
puts sum.to_f
I converted the strings representing the prices to Rationals to avoid
rounding errors.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs