Skip to content

Instantly share code, notes, and snippets.

@KeithYJohnson
Created August 21, 2015 16:23
Show Gist options
  • Save KeithYJohnson/3b93e92e235f2354cb49 to your computer and use it in GitHub Desktop.
Save KeithYJohnson/3b93e92e235f2354cb49 to your computer and use it in GitHub Desktop.
def nested_delete_keys(*invalid_keys)
invalid_keys = invalid_keys.flatten
keys.each do |key|
key = key.to_s
if key.to_s.in?(invalid_keys)
delete(key)
elsif self[key].is_a?(Hash)
self[key].nested_delete_keys(invalid_keys.flatten)
elsif self[key].is_a?(Array) && self[key].first.is_a?(Hash)
self[key].first.nested_delete_keys(*invalid_keys)
end
end
self
end
def nested_only(valid_keys_array)
valid_keys_array = valid_keys_array.flatten
keys.each do |key|
if self[key].is_a?(Hash)
self[key].nested_only(valid_keys_array)
elsif self[key].is_a?(Array) && self[key].first.is_a?(Hash)
self[key].first.nested_only(valid_keys_array)
elsif !key.in?(valid_keys_array)
self.delete(key)
end
end
self
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment