-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenumerable.rb
179 lines (139 loc) · 4.04 KB
/
enumerable.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# rubocop : disable Metrics/ModuleLength
# rubocop : disable Metrics/PerceivedComplexity
# rubocop : disable Metrics/CyclomaticComplexity
module Enumerable
def my_each
return to_enum(:my_each) unless block_given?
arr = to_a
i = 0
while i < arr.length
yield(arr[i])
i += 1
end
self
end
def my_each_with_index
return to_enum(:my_each_with_index) unless block_given?
arr = to_a
i = 0
while i < arr.length
yield(arr[i], i)
i += 1
end
self
end
def my_select
new_arr = []
return to_enum(:my_select) unless block_given?
my_each do |element|
result = yield(element)
new_arr.push(element) if result
end
new_arr
end
def my_all?(parameter = false)
my_each do |item|
if block_given?
return false unless yield(item)
elsif !block_given? && parameter == false
return false unless item
elsif parameter && parameter.class == Class
return false unless item.is_a?(parameter)
elsif parameter && parameter.class == Regexp
return false unless item.match(parameter)
elsif parameter
return false unless item == parameter
end
end
true
end
def my_any?(parameter = false)
my_each do |item|
if block_given? && parameter == false
return true if yield(item)
elsif !block_given? && parameter == false
return true if item
elsif parameter && parameter.class == Class
return true if item.is_a?(parameter)
elsif parameter && parameter.class == Regexp
return true if item.match(parameter)
elsif parameter
return true if item == parameter
end
end
false
end
def my_none?(parameter = false)
my_each do |item|
if block_given? && parameter == false
return false if yield(item)
elsif !block_given? && parameter == false
return false if item
elsif parameter && parameter.class == Class
return false if item.is_a?(parameter)
elsif parameter && parameter.class == Regexp
return false if item.match(parameter)
elsif parameter
return false if item == parameter
end
end
true
end
def my_count(parameter = false)
result = 0
arr = to_a
my_each do |item|
if !block_given? && parameter
result += 1 if item == parameter
elsif block_given? && parameter == false
result += 1 if yield(item) == true
end
end
return arr.size if !block_given? && parameter == false
result
end
def my_map(my_proc = false)
new_arr = []
return to_enum(:my_map) unless block_given?
my_each do |item|
if block_given? && my_proc == false
new_arr.push yield(item)
elsif block_given? && my_proc.class == Proc
new_arr.push my_proc.call(item)
elsif my_proc && my_proc.class == Symbol
new_arr.push item.my_proc
elsif my_proc
new_arr.push my_proc.call(item)
end
end
new_arr
end
def my_inject(arg_one = false, arg_two = false)
return if !block_given? && arg_one && arg_one.class != Symbol && arg_two == false
raise LocalJumpError if !block_given? && arg_one == false
arr = to_a
accumulator = arr[0]
i = 0
while i < arr.length - 1
if block_given? && arg_one == false && arg_two == false
accumulator = yield(accumulator, arr[i + 1])
elsif !block_given? && arg_one.class == Symbol
accumulator = accumulator.send(arg_one, arr[i + 1])
elsif !block_given? && arg_one && arg_two.class == Symbol
accumulator = accumulator.send(arg_two, arr[i + 1])
end
i += 1
end
accumulator *= arg_one if arg_one && arg_two == false && arg_one.class != Symbol
accumulator = accumulator.send(arg_two, arg_one) if arg_one && arg_two.class == Symbol
accumulator
end
end
def multiply_els(arr)
arr.my_inject do |a, b|
a * b
end
end
# rubocop : enable Metrics/ModuleLength
# rubocop : enable Metrics/PerceivedComplexity
# rubocop : enable Metrics/CyclomaticComplexity