Skip to content

Instantly share code, notes, and snippets.

@pje
Last active December 12, 2015 12:39
Show Gist options
  • Save pje/4773784 to your computer and use it in GitHub Desktop.
Save pje/4773784 to your computer and use it in GitHub Desktop.
Extension for Ruby's Hash class: - `Hash#replace_key!` - `Hash#replace_key`
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