Method: Hash#rename

Defined in:
lib/sc-core-ext/hash.rb

#rename(to) ⇒ Object

Takes a hash whose keys must match keys in this hash. Those keys will be renamed to match the corresponding value in the specified hash.

Keys not found are ignored.

Returns self.

Example:

{ :a => 1 }.rename(:a => :b)
  => {:b => 1}


77
78
79
80
81
82
83
# File 'lib/sc-core-ext/hash.rb', line 77

def rename(to)
  merge!(inject(self.class.new) do |hash, (old_key, value)|
    hash[to[old_key] || old_key] = value
    delete(old_key)
    hash
  end)
end