Code Explainers

Code explainers tagged #nested-hashes

ruby
def build_pivot_table(sales)
  pivot = sales.each_with_object(Hash.new { |h, k| h[k] = Hash.new(0.0) }) do |record, table|
    region = record.fetch(:region)
    quarter = record.fetch(:quarter)

Building a pivot table in Ruby

aggregation nested-hashes default-values
Intermediate 9 steps