Skip to content

Instantly share code, notes, and snippets.

@michaeldelorenzo
Created May 15, 2014 16:42
Show Gist options
  • Save michaeldelorenzo/7768c7c67b88229ec16c to your computer and use it in GitHub Desktop.
Save michaeldelorenzo/7768c7c67b88229ec16c to your computer and use it in GitHub Desktop.
Recursively removes key-value pairs from a Hash using lambda.
remove_id_attr = lambda do |obj|
if obj.is_a?(Hash)
obj.keys.each do |_k|
if _k.to_s.eql?('_id')
obj.delete(_k)
elsif obj[_k].is_a?(Hash)
remove_id_attr.call(obj[_k])
elsif obj[_k].is_a?(Array)
obj[_k].each{|o| remove_id_attr.call(o)}
end
end
end
end
# remove _id attributes on sub-documents
remove_id_attr.call(demo_profile_doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment