Hash.each / map / select / reject
Methods for iterating over, transforming, and filtering each key-value pair in a hash. Just like with arrays, you can use each, map, select, and reject — the difference is that the block takes two variables: a key and a value.
Syntax
# Iterates over each key-value pair.
hash.each { |key, value| ... }
hash.each_pair { |key, value| ... }
# Returns an array by transforming each pair.
hash.map { |key, value| transformed_value }
# Returns a new hash containing only pairs that match the condition.
hash.select { |key, value| condition }
hash.filter { |key, value| condition } # alias for select
# Returns a new hash with matching pairs removed.
hash.reject { |key, value| condition }
Method List
| Method | Description |
|---|---|
| each { |k, v| ... } | Iterates over each key-value pair in order. Returns the original hash. |
| each_pair { |k, v| ... } | An alias for each. |
| each_key { |k| ... } | Iterates over keys only. |
| each_value { |v| ... } | Iterates over values only. |
| map { |k, v| ... } | Returns an array of results from transforming each pair with the block. |
| select { |k, v| ... } | Returns a new hash containing only the pairs for which the block returns true. |
| reject { |k, v| ... } | Returns a new hash with the pairs for which the block returns true removed. |
| count { |k, v| ... } | Returns the number of pairs matching the condition. Without a block, returns the total number of pairs. |
| any? { |k, v| ... } | Returns true if at least one pair satisfies the condition. |
| all? { |k, v| ... } | Returns true if all pairs satisfy the condition. |
Sample Code
scores = { taro: 85, hanako: 92, jiro: 68, saburo: 77 }
# each: print each name and score
scores.each { |name, score| puts "#{name}: #{score} points" }
# each_key / each_value
scores.each_key { |k| print "#{k} " } # taro hanako jiro saburo
puts
scores.each_value { |v| print "#{v} " } # 85 92 68 77
puts
# map: add 10 points to every score and build a new hash
boosted = scores.map { |name, score| [name, score + 10] }.to_h
puts boosted.inspect
# {:taro=>95, :hanako=>102, :jiro=>78, :saburo=>87}
# select: keep only students who scored 80 or above
passed = scores.select { |_, score| score >= 80 }
puts passed.inspect
# {:taro=>85, :hanako=>92}
# reject: remove low scores (opposite of select)
not_low = scores.reject { |_, score| score < 75 }
puts not_low.inspect
# {:taro=>85, :hanako=>92, :saburo=>77}
# count / any? / all?
puts scores.count { |_, s| s >= 80 } # 2 (number of passing students)
puts scores.any? { |_, s| s >= 90 } # true (is there anyone with 90+?)
puts scores.all? { |_, s| s >= 60 } # true (did everyone score 60+?)
Notes
Using each, map, and select on a hash feels similar to using them on an array, but the key difference is that the block receives two variables — a key and a value. If you use a single block variable, the key-value pair is passed as a two-element array.
map returns an array when called on a hash. If you want the result as a hash, you need to chain to_h at the end, or use the each_with_object({}) pattern. From Ruby 2.6 onward, using transform_values / transform_keys is often more intuitive.
When a block variable is not needed, it is conventional to use _ (underscore) as a placeholder (e.g., reject { |_, score| score < 75 }). This makes it clear that the variable is intentionally ignored.
If you find any errors or copyright issues, please contact us.