Skip to content

Instantly share code, notes, and snippets.

@maxcal
Created December 3, 2022 11:56
Show Gist options
  • Save maxcal/468d8b3998ab5356e83e42b381593120 to your computer and use it in GitHub Desktop.
Save maxcal/468d8b3998ab5356e83e42b381593120 to your computer and use it in GitHub Desktop.
SO 74666328
irb(main):032:0> value={:next_page_token=>nil, :group_definitions=>nil, :results_by_time=>[{:time_period=>{:start=>"2022-10-01", :end=>"2022-11-01"}, :total=>{"BlendedCost"=>{:amount=>"49.1803785401", :unit=>"USD"}}, :groups=>[], :estimated=>false}, {:time_period=>{:start=>"2022-11-01", :end=>"2022-12-01"}, :total=>{"BlendedCost"=>{:amount=>"79.0698396954", :unit=>"USD"}}, :groups=>[], :estimated=>false}, {:time_period=>{:start=>"2022-12-01", :end=>"2022-12-03"}, :total=>{"BlendedCost"=>{:amount=>"2.6918272089", :unit=>"USD"}}, :groups=>[], :estimated=>true}], :dimension_value_attributes=>[]}
=>
{:next_page_token=>nil,
...
irb(main):033:0* sum = value[:results_by_time]&.sum do |hash|
irb(main):034:0* hash.dig(:total, 'BlendedCost', :amount)&.to_f || 0
irb(main):035:-> end
=> 130.9420454444
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment