Collections
Hash Iteration
A hash stores named values and can be iterated to compute a total.
hash iteration
`each` visits every key and value pair in a hash.
Hash Iteration
hash_iteration.rb
Replay: real traced execution (multi-file project)
prices = { apple: 2, banana: 3, pear: 4 }
lookup = :pear
total = 0
prices.each do |name, price|
total += price
puts "#{name}=#{price}"
end
found = prices[lookup] || 0
puts "total=#{total}"
puts "lookup=#{lookup}"
puts "found=#{found}"
prices = { apple: 2, banana: 3, pear: 4 }
lookup = :apple
total = 0
prices.each do |name, price|
total += price
puts "#{name}=#{price}"
end
found = prices[lookup] || 0
puts "total=#{total}"
puts "lookup=#{lookup}"
puts "found=#{found}"
prices = { apple: 2, banana: 3, pear: 4 }
lookup = :orange
total = 0
prices.each do |name, price|
total += price
puts "#{name}=#{price}"
end
found = prices[lookup] || 0
puts "total=#{total}"
puts "lookup=#{lookup}"
puts "found=#{found}"
prices ← {:apple=>2, :banana=>3, :pear=>4}, lookup ← pear, total ← 0
1prices→ {:apple=>2, :banana=>3, :pear=>4} = { apple: 2, banana: 3, pear: 4 }2lookup→ pear = :pear #@lookup=:apple, :orange34total→ 0 = 05prices{:apple=>2, :banana=>3, :pear=>4}.each do |name, price|6 total += price7 puts "#{name}=#{price}"8endtotal ← 2
pass 1 of 34total = 05prices.each do |nameapple, price2|6 total→ 2 += price27 puts "#{nameapple}=#{price2}"8endoutputapple=2All 3 passes — pass 1 is the card above pass namepricepricestotal1 apple 2 — 0 → 2 2 banana 3 — 2 → 5 3 pear 4 {:apple=>2, :banana=>3, :pear=>4} 5 → 9 found ← 4
10found→ 4 = prices[lookup]4 || 01112puts "total=#{total9}"13puts "lookup=#{lookuppear}"14puts "found=#{found4}"outputtotal=9 lookup=pear found=4
prices ← {:apple=>2, :banana=>3, :pear=>4}, lookup ← apple, total ← 0
1prices→ {:apple=>2, :banana=>3, :pear=>4} = { apple: 2, banana: 3, pear: 4 }2lookup→ apple = :apple34total→ 0 = 05prices{:apple=>2, :banana=>3, :pear=>4}.each do |name, price|6 total += price7 puts "#{name}=#{price}"8endtotal ← 2
pass 1 of 34total = 05prices.each do |nameapple, price2|6 total→ 2 += price27 puts "#{nameapple}=#{price2}"8endoutputapple=2All 3 passes — pass 1 is the card above pass namepricepricestotal1 apple 2 — 0 → 2 2 banana 3 — 2 → 5 3 pear 4 {:apple=>2, :banana=>3, :pear=>4} 5 → 9 found ← 2
10found→ 2 = prices[lookup]2 || 01112puts "total=#{total9}"13puts "lookup=#{lookupapple}"14puts "found=#{found2}"outputtotal=9 lookup=apple found=2
prices ← {:apple=>2, :banana=>3, :pear=>4}, lookup ← orange, total ← 0
1prices→ {:apple=>2, :banana=>3, :pear=>4} = { apple: 2, banana: 3, pear: 4 }2lookup→ orange = :orange34total→ 0 = 05prices{:apple=>2, :banana=>3, :pear=>4}.each do |name, price|6 total += price7 puts "#{name}=#{price}"8endtotal ← 2
pass 1 of 34total = 05prices.each do |nameapple, price2|6 total→ 2 += price27 puts "#{nameapple}=#{price2}"8endoutputapple=2All 3 passes — pass 1 is the card above pass namepricepricestotal1 apple 2 — 0 → 2 2 banana 3 — 2 → 5 3 pear 4 {:apple=>2, :banana=>3, :pear=>4} 5 → 9 found ← 0
10found→ 0 = prices[lookup](empty) || 01112puts "total=#{total9}"13puts "lookup=#{lookuporange}"14puts "found=#{found0}"outputtotal=9 lookup=orange found=0