Skip to content

Instantly share code, notes, and snippets.

@nthj
Created April 5, 2011 01:31
Show Gist options
  • Save nthj/902856 to your computer and use it in GitHub Desktop.
Save nthj/902856 to your computer and use it in GitHub Desktop.
Ruby Hash filter (Array#map for Hashes)
# This isn't mine
# I can't remember where I found it, but mad props to whoever created it
class Hash
def filter
result = self.map do |k, v|
r = yield v
[k, r]
end
Hash[*result.flatten]
end
def filter! &block
replace self.filter &block
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment