Skip to content

Instantly share code, notes, and snippets.

@rmcafee
Created May 19, 2009 18:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rmcafee/114288 to your computer and use it in GitHub Desktop.
Save rmcafee/114288 to your computer and use it in GitHub Desktop.
Extend Ruby Hash with Useful Methods
class Hash
def except(*blacklist)
{}.tap do |h|
(keys - blacklist).each { |k| h[k] = self[k] }
end
end
def only(*whitelist)
{}.tap do |h|
(keys & whitelist).each { |k| h[k] = self[k] }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment