Created
April 6, 2009 19:49
-
-
Save sneakin/90899 to your computer and use it in GitHub Desktop.
Hash#remap – alters a Hash's keys using a supplied mapping.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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