Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Hash#transform_keys

() -> ::Enumerator[K, ::Hash[untyped, V]]
[K2] (::hash[::Hash::_Key, K2] replacements) -> ::Hash[K | K2, V]
[K2] (::hash[::Hash::_Key, K2] replacements) { (K old_key) -> K2 } -> ::Hash[K2, V]
[K2] () { (K key) -> K2 } -> ::Hash[K2, V]
transform_keys {|old_key| ... } → new_hash
transform_keys(other_hash) → new_hash
transform_keys(other_hash) {|old_key| ...} → new_hash
transform_keys → new_enumerator

With an argument, a block, or both given, derives a new hash new_hash from self, the argument, and/or the block; all, some, or none of its keys may be different from those in self.

With a block given and no argument, new_hash has keys determined only by the block.

For each key/value pair old_key/value in self, calls the block with old_key; the block’s return value becomes new_key; sets new_hash[new_key] = value; a duplicate key overwrites:

Example
h = {foo: 0, bar: 1, baz: 2}
h.transform_keys {|old_key| old_key.to_s }
# => {"foo" => 0, "bar" => 1, "baz" => 2}
h.transform_keys {|old_key| 'xxx' }
# => {"xxx" => 2}

With argument other_hash given and no block, new_hash may have new keys provided by other_hash and unchanged keys provided by self.

For each key/value pair old_key/old_value in self, looks for key old_key in other_hash:

  • If old_key is found, its value other_hash[old_key] is taken as new_key; sets new_hash[new_key] = value; a duplicate key overwrites:

    Example
    h = {foo: 0, bar: 1, baz: 2}
    h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO)
    # => {FOO: 0, BAR: 1, BAZ: 2}
    h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO)
    # => {FOO: 2}
    
  • If old_key is not found, sets new_hash[old_key] = value; a duplicate key overwrites:

    Example
    h = {foo: 0, bar: 1, baz: 2}
    h.transform_keys({})
    # => {foo: 0, bar: 1, baz: 2}
    h.transform_keys(baz: :foo)
    # => {foo: 2, bar: 1}
    

Unused keys in other_hash are ignored:

Example
h = {foo: 0, bar: 1, baz: 2}
h.transform_keys(bat: 3)
# => {foo: 0, bar: 1, baz: 2}

With both argument other_hash and a block given, new_hash has new keys specified by other_hash or by the block, and unchanged keys provided by self.

For each pair old_key and value in self:

  • If other_hash has key old_key (with value new_key), does not call the block for that key; sets new_hash[new_key] = value; a duplicate key overwrites:

    Example
    h = {foo: 0, bar: 1, baz: 2}
    h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' }
    # => {FOO: 0, BAR: 1, BAZ: 2}
    
  • If other_hash does not have key old_key, calls the block with old_key and takes its return value as new_key; sets new_hash[new_key] = value; a duplicate key overwrites:

    Example
    h = {foo: 0, bar: 1, baz: 2}
    h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse }
    # => {"oof" => 0, "rab" => 1, BAZ: 2}
    h.transform_keys(baz: :BAZ) {|key| 'ook' }
    # => {"ook" => 1, BAZ: 2}
    

With no argument and no block given, returns a new Enumerator.

Related: see Methods for Transforming Keys and Values.

Ruby version changes

  1. 3.0
See also #transform_keys! #to_s #to_proc #transform_values also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0