Created
April 5, 2011 01:31
-
-
Save nthj/902856 to your computer and use it in GitHub Desktop.
Ruby Hash filter (Array#map for Hashes)
This file contains 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
# 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