-
Notifications
You must be signed in to change notification settings - Fork 105
/
iterator.rb
53 lines (44 loc) · 990 Bytes
/
iterator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Iterator helps you to iterate through a complex object using
# an iterator method
class Parent
attr_reader :first_name
def initialize(first_name, gender)
@first_name = first_name
@gender = gender
end
end
class Child < Parent
end
class Family
def initialize(surname)
@surname = surname
@children = []
end
def add_father(first_name)
@father = Parent.new first_name, "M"
end
def add_mother(first_name)
@mother = Parent.new first_name, "F"
end
def add_child(first_name, gender)
@children << Child.new(first_name, gender)
end
def each_member
[@father, @mother, @children].flatten.each do |member|
yield member
end
end
end
# Usage
family = Family.new "Jackson"
family.add_father("Robert")
family.add_mother("Susan")
family.add_child("Lucas", "M")
family.add_child("James", "M")
family.add_child("Rose", "F")
family.each_member{ |member| puts member.first_name }
# => Robert
# Susan
# Lucas
# James
# Rose