Skip to content

Instantly share code, notes, and snippets.

@sneakin
Created April 6, 2009 19:49
Show Gist options
  • Save sneakin/90899 to your computer and use it in GitHub Desktop.
Save sneakin/90899 to your computer and use it in GitHub Desktop.
Hash#remap – alters a Hash's keys using a supplied mapping.
class Hash
# Given a hash that maps keys to the new key, this method will return a new
# Hash whose keys are contained in the map Hash, but whose values come from
# the Hash #remap was called on.
#
# Ex: { 1 => 2, 2 => 3 }.remap(1 => 100, 2 => 200) # => { 100 => 2, 200 => 3 }
#
def remap(mapping)
ret = Hash.new
mapping.each do |src, dest|
ret[dest] = self[src]
end
ret
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment