Created
November 15, 2017 18:59
-
-
Save steveevers/9d584aed053b9b31467101807462a94c to your computer and use it in GitHub Desktop.
Depth first recursive versions of Hash::except
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
class Hash | |
def except_nested(key) | |
r = Marshal.load(Marshal.dump(self)) | |
r.except_nested!(key) | |
end | |
def except_nested!(key) | |
self.except!(key) | |
self.each do |_, v| | |
v.except_nested!(key) if v.is_a?(Hash) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the case that removing keys inside array is needed:
The
except!
was replaced by reject, since the first is exclusive for newer ruby verions.