Created
February 14, 2014 17:41
-
-
Save beautifulcode/9005497 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 Person | |
def initialize(name, children = []) | |
@children = children | |
@descendents = [] | |
@name = name | |
end | |
def get_all_descendents | |
children.each do |child| | |
@descendents << child | |
@descendents << child.get_all_descendents if child.children.size > 0 | |
end | |
@descendents.flatten.uniq | |
end | |
def children | |
@children || [] | |
end | |
def to_s | |
@name | |
end | |
end | |
son = Person.new('Son') | |
father = Person.new('Father', [son]) | |
grandfather = Person.new('GrandFather', [father]) | |
granduncle = Person.new('GrandUncle', []) | |
great_grandfather = Person.new('GreatGrandfather', [grandfather, granduncle]) | |
puts great_grandfather.get_all_descendents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment