-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path13-collecting_parameter.rb
More file actions
57 lines (51 loc) · 1.29 KB
/
13-collecting_parameter.rb
File metadata and controls
57 lines (51 loc) · 1.29 KB
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
54
55
56
57
# How do you return a collection that is the collaborative result of several methods?
#
# Add a parameter that collects their results to of all the submethods.
# Original
class People
def married_man_and_unmarried_woman
result = Array.new
self.people.each do |person|
result << person if person.man? and person.married?
end
self.people.each do |person|
result << person if person.woman? and person.unmarried?
end
return result
end
end
# Extract Composed Method
class People
def married_man
result = Array.new
self.people.each do |person|
result << person if person.man? and person.married?
end
return result
end
def unmarried_woman
result = Array.new
self.people.each do |person|
result << person if person.woman? and person.unmarried?
end
return result
end
end
class People
def married_man_and_unmarried_woman
result = Array.new
add_married_man_to result
add_unmarried_woman_to result
return result
end
def add_married_man_to collection
self.people.each do |person|
collection << person if person.man? and person.married?
end
end
def add_unmarried_woman_to collection
self.people.each do |person|
collection << person if person.woman? and person.unmarried?
end
end
end