Last active
January 3, 2016 07:19
-
-
Save maxhodak/8428717 to your computer and use it in GitHub Desktop.
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
foo = { "bar" => "baz" } | |
def quuxify(bar) | |
bar["bar"] = "quux" | |
end | |
foo #=> {"bar"=>"baz"} | |
quuxify(foo) | |
foo #=> {"bar"=>"quux"} |
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
def linearize(data) | |
nodes = {} | |
traverse = lambda do |node| | |
if node.is_a?(Hash) | |
node.each do |key, val| | |
if val.is_a?(Hash) | |
node[key] = { | |
"ref" => traverse.call(val) | |
} | |
end | |
if val.is_a?(Array) | |
traverse.call(val) | |
end | |
end | |
elsif node.is_a?(Array) | |
node.each_with_index do |item, idx| | |
node[idx] = { | |
"ref" => traverse.call(item) | |
} | |
end | |
end | |
nodes[nodes.length] = node | |
return nodes.length - 1 | |
end | |
traverse.call(data) | |
nodes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment