Hash#transform_values
hash.transform_values { |value| block } -> hash Hash · Added in v2.5 · Updated March 16, 2026 · Hash Methods Hash#transform_values creates a new hash by transforming each value using a block while keeping keys unchanged. It’s the counterpart to Hash#transform_keys when you need to modify values rather than keys.
Syntax
hash.transform_values { |value| block }
The block receives each value and should return the new value to use.
Basic Usage
Simple value transformation
scores = { alice: 95, bob: 82, carol: 78 }
scores.transform_values { |score| score + 5 }
# => { alice: 100, bob: 87, carol: 83 }
Convert values to strings
prices = { apple: 100, banana: 200, orange: 150 }
prices.transform_values(&:to_s)
# => { "apple" => "100", "banana" => "200", "orange" => "150" }
Type conversion
data = { count: "42", ratio: "3.14", flag: "true" }
data.transform_values { |v| eval(v) }
# => { count: 42, ratio: 3.14, flag: true }
Practical Examples
Normalize data
input = { name: " ALICE ", city: "london", status: "ACTIVE" }
input.transform_values { |v| v.strip.downcase }
# => { name: "alice", city: "london", status: "active" }
Apply calculations
products = { laptop: 999, mouse: 29, keyboard: 79 }
products.transform_values { |price| (price * 0.9).round }
# => { laptop: 899, mouse: 26, keyboard: 71 }
Sanitize user input
fields = { username: " alice ", bio: "Hello\nWorld", website: "HTTP://EXAMPLE.COM" }
fields.transform_values do |value|
value.to_s.strip.gsub(/^https?:\/\//i, "").downcase
end
# => { username: "alice", bio: "Hello\nWorld", website: "example.com" }
Comparison with Related Methods
transform_values vs map
Using transform_values is more idiomatic than using map to transform values:
hash = { a: 1, b: 2, c: 3 }
# Less idiomatic
hash.map { |k, v| [k, v * 10] }.to_h
# => { a: 10, b: 20, c: 30 }
# More idiomatic
hash.transform_values { |v| v * 10 }
# => { a: 10, b: 20, c: 30 }
transform_values vs transform_values!
The bang version transform_values! modifies the hash in place:
config = { timeout: 30, retries: 5 }
config.transform_values! { |v| v * 2 }
# config is now { timeout: 60, retries: 10 }
Use transform_values when you want an immutable operation (returning a new hash).
transform_values vs transform_keys
Transform values operates on values while keeping keys the same:
data = { name: "Alice", age: 30 }
data.transform_keys(&:to_s)
# => { "name" => "Alice", "age" => 30 }
data.transform_values(&:to_s)
# => { name: "30", age: "30" }
Chaining with Other Methods
Combine with transform_keys
input = { FIRST_NAME: "alice", LAST_NAME: "smith", AGE: 30 }
input
.transform_keys(&:downcase)
.transform_values { |v| v.is_a?(String) ? v.capitalize : v }
# => { first_name: "Alice", last_name: "Smith", age: 30 }
Combine with select
users = { alice: { active: true, score: 95 }, bob: { active: false, score: 82 } }
users.transform_values { |v| v[:score] if v[:active] }.compact
# => { alice: 95 }
Combine with merge
defaults = { debug: false, timeout: 30 }
defaults.transform_values(&:to_s).merge({ "debug" => "true", "timeout" => "60" })
# => { "debug" => "true", "timeout" => "60" }
In-Place Modification with transform_values!
If you need to modify the original hash rather than creating a new one, use transform_values!:
cache = { user_1: "cached_data", user_2: "more_cached" }
cache.transform_values!(&:upcase)
# cache is now { user_1: "CACHED_DATA", user_2: "MORE_CACHED" }
This is useful when working with configuration objects that you want to modify directly.
Edge Cases
Empty hash
{}.transform_values { |v| v.to_s }
# => {}
Mixed value types
mixed = { a: 1, b: "two", c: nil }
mixed.transform_values { |v| v.to_s }
# => { a: "1", b: "two", c: "" }
Block not provided
Without a block, transform_values returns an enumerator:
hash = { a: 1, b: 2, c: 3 }
hash.transform_values.map { |v| v * 10 }
# => [10, 20, 30]
Ruby Version Notes
- Ruby 2.5: Introduced
transform_values - Ruby 2.5: Introduced
transform_values! - Ruby 2.5: Added blockless form returning enumerator
See Also
- Hash#transform_keys — Transform hash keys
- Hash#values — Get all hash values as an array
- Hash#merge — Merge hashes together