0Pricing
Ruby Academy · Lesson

Hash Iteration

each, map, select.

Hash Iteration is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Iterating with each

The each method visits every pair. The block receives the key and value.

scores = { math: 90, art: 80 }
scores.each do |subject, score|
  puts "#{subject}: #{score}"
end

each_pair

each_pair is an alias of each for hashes. Use whichever reads clearer.

h = { a: 1, b: 2 }
h.each_pair { |k, v| puts "#{k}=#{v}" }

Iterating Keys Only

each_key loops over keys when you do not need the values.

h = { name: "Ada", age: 36 }
h.each_key { |k| puts k }

Iterating Values Only

each_value loops over values directly.

h = { a: 10, b: 20 }
total = 0
h.each_value { |v| total += v }
puts total

Transforming with map

map on a hash returns an array. Each pair is yielded as key and value.

prices = { pen: 2, book: 5 }
labels = prices.map { |item, p| "#{item}: $#{p}" }
puts labels.inspect

transform_values

transform_values returns a new hash with each value changed but keys kept.

prices = { pen: 2, book: 5 }
doubled = prices.transform_values { |p| p * 2 }
puts doubled

transform_keys

transform_keys changes keys while keeping values. Useful for converting strings to symbols.

h = { "a" => 1, "b" => 2 }
puts h.transform_keys(&:to_sym)

Filtering with select

select (alias filter) keeps pairs where the block is true and returns a new hash.

scores = { a: 90, b: 50, c: 70 }
passing = scores.select { |k, v| v >= 70 }
puts passing

Rejecting Pairs

reject is the opposite of select. It drops pairs where the block is true.

scores = { a: 90, b: 50 }
puts scores.reject { |k, v| v < 60 }

Finding and Counting

find returns the first matching pair as an array. count with a block counts matches.

h = { a: 1, b: 2, c: 3 }
puts h.find { |k, v| v == 2 }.inspect
puts h.count { |k, v| v.odd? }

Summing Values

Combine values with sum for totals, or use each_with_object for custom aggregation.

cart = { apple: 3, pear: 5 }
puts cart.values.sum

Quick Check

Check your hash iteration knowledge.

Recap

You can now iterate hashes:

  • each, each_key, each_value
  • map, transform_values, transform_keys
  • select/reject to filter
  • find, count, and summing values

Next: default values and merging.

h = { a: 1, b: 2, c: 3 }
puts h.select { |k, v| v.odd? }

Frequently asked questions

Is the “Hash Iteration” lesson free?

Yes — the full text of “Hash Iteration” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Hash Iteration”?

each, map, select. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Hash Iteration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Symbols vs Strings
  2. Creating and Reading Hashes
  3. Hash Iteration
  4. Default Values and Merging
← Back to Ruby Academy