Created
June 28, 2017 20:04
-
-
Save roalcantara/e576b68838ce9aeb9ef0df700ff6c284 to your computer and use it in GitHub Desktop.
Ruby > Remove has values recursively
This file contains hidden or 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
event = { | |
"level" => 99, | |
"msgid" => "97bf040b.ba1078", | |
"pid" => 21, | |
"source" => "f7d04af7b740/node-red", | |
"message" => { | |
"res" => { | |
"_res" => "[Circular]" | |
}, | |
"payload" => "[Circular]" | |
} | |
} | |
def clean_up(event) | |
if event&.is_a?(Hash) | |
event.each_pair do |key, value| | |
if value == '[Circular]' | |
event.delete(key) | |
else | |
clean_up(value) | |
end | |
end | |
elsif event&.is_a?(Array) | |
event = event.collect { |item| clean_up(item) } | |
end | |
event | |
end | |
clean_up(event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment