Last active
December 12, 2015 12:39
-
-
Save pje/4773784 to your computer and use it in GitHub Desktop.
Extension for Ruby's Hash class: - `Hash#replace_key!` - `Hash#replace_key`
This file contains hidden or 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 | |
def replace_key!(a, b) | |
if self[a] | |
self[b] = self.delete(a) | |
end | |
self | |
end | |
def replace_key(a, b) | |
if self[a] | |
self.dup.replace_key!(a, b) | |
else | |
self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment