Created
September 11, 2015 12:05
-
-
Save joker1007/ca828799395be9e4ef92 to your computer and use it in GitHub Desktop.
This file contains 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
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