Skip to content

Instantly share code, notes, and snippets.

@tcopeland
Last active September 6, 2017 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcopeland/9dd1c4d95a8249eaf165d115a7fd0d2f to your computer and use it in GitHub Desktop.
Save tcopeland/9dd1c4d95a8249eaf165d115a7fd0d2f to your computer and use it in GitHub Desktop.
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