Ruby Academy · Lesson

Hash Iteration

each, map, select.

Lesson 3 of 413 steps

Hash Iteration is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

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? }
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Hash Iteration” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Hash Iteration”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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