Last active
September 6, 2017 18:04
-
-
Save tcopeland/9dd1c4d95a8249eaf165d115a7fd0d2f to your computer and use it in GitHub Desktop.
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
a = {zzz: [{b: 1, c: 2}, {b:3, c:4}]} | |
# slight variant on https://stackoverflow.com/questions/10676608/ruby-deleting-all-instances-of-a-particular-key-from-hash-of-hashes | |
class Hash | |
def deep_reject_key!(key) | |
keys.each {|k| delete(k) if k == key || self[k] == self[key] } | |
values.each do |v| | |
v.deep_reject_key!(key) if v.is_a? Hash | |
v.each {|i| i.deep_reject_key!(key) if i.is_a? Hash } if v.is_a? Array | |
end | |
self | |
end | |
end | |
a.deep_reject_key!(:b) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment