Skip to content

Instantly share code, notes, and snippets.

@maxhodak
Last active January 3, 2016 07:19
Show Gist options
  • Save maxhodak/8428717 to your computer and use it in GitHub Desktop.
Save maxhodak/8428717 to your computer and use it in GitHub Desktop.
foo = { "bar" => "baz" }
def quuxify(bar)
bar["bar"] = "quux"
end
foo #=> {"bar"=>"baz"}
quuxify(foo)
foo #=> {"bar"=>"quux"}
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