Skip to content
instance method

Hash#transform_keys!

() -> ::Enumerator[K, self]
() { (K) -> K } -> self
transform_keys! {|old_key| ... } → self
transform_keys!(other_hash) → self
transform_keys!(other_hash) {|old_key| ...} → self
transform_keys! → new_enumerator

With an argument, a block, or both given, derives keys from the argument, the block, and self; all, some, or none of the keys in self may be changed.

With a block given and no argument, derives keys only from the block; all, some, or none of the keys in self may be changed.

For each key/value pair old_key/value in self, calls the block with old_key; the block’s return value becomes new_key; removes the entry for old_key: self.delete(old_key); sets self[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 = {foo: 0, bar: 1, baz: 2}
h.transform_keys! {|old_key| 'xxx' }
# => {"xxx" => 2}

With argument other_hash given and no block, derives keys for self from other_hash and self; all, some, or none of the keys in self may be changed.

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

  • If old_key is found, takes value other_hash[old_key] as new_key; removes the entry for old_key: self.delete(old_key); sets self[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 = {foo: 0, bar: 1, baz: 2}
    h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO)
    # => {FOO: 2}
    
  • If old_key is not found, does nothing:

    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, derives keys from other_hash, the block, and self; all, some, or none of the keys in self may be changed.

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; removes the entry for old_key: self.delete(old_key); sets self[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; removes the entry for old_key: self.delete(old_key); sets self[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 = {foo: 0, bar: 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