Skip to content

Instantly share code, notes, and snippets.

@joker1007
Created September 11, 2015 12:05
Show Gist options
  • Save joker1007/ca828799395be9e4ef92 to your computer and use it in GitHub Desktop.
Save joker1007/ca828799395be9e4ef92 to your computer and use it in GitHub Desktop.
class TestObject
def initialize(name)
@name = name
@children = []
end
def add_child(child)
@children << child
self
end
def to_h
{name: @name, children: @children.map(&:to_h)}
end
def to___h
{name: @name, children: @children}
end
end
root = TestObject.new("name1")
root.add_child(TestObject.new("child1").add_child(TestObject.new("child1-1")))
root.add_child(TestObject.new("child2"))
root.add_child(TestObject.new("child3"))
root.add_child(TestObject.new("child4"))
p root.to_h
p root.to___h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment