Skip to content

Instantly share code, notes, and snippets.

@cesarfigueroa
Last active December 17, 2015 22:19
Show Gist options
  • Save cesarfigueroa/5681278 to your computer and use it in GitHub Desktop.
Save cesarfigueroa/5681278 to your computer and use it in GitHub Desktop.
Hash extensions for including and excluding keys.
class Hash
def only(*keys)
self.select { |key, _| keys.include?(key) }
end
def except(*keys)
self.reject { |key, _| keys.include?(key) }
end
end
@cesarfigueroa
Copy link
Author

For the mutable versions, use select! and reject! instead, respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment