-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
205 lines (191 loc) · 4.07 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# rubocop:disable Style/For
# rubocop:disable Metrics/ModuleLength
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
module Enumerable
def my_each
return enum_for unless block_given?
for i in 0..to_a.length - 1 do
yield to_a[i]
end
self
end
def my_each_with_index
return enum_for unless block_given?
for i in 0..to_a.length - 1 do
yield to_a[i], i
end
self
end
def my_select
return enum_for unless block_given?
new_arr = to_a
new_arr_two = []
new_arr.my_each { |i| new_arr_two << i if yield i }
new_arr_two
end
def my_all?(*arg)
if block_given?
my_each do |i|
return false unless yield i
end
elsif arg[0]
if arg[0].is_a?(Regexp)
my_each do |i|
return false unless arg[0].match?(i)
end
elsif arg[0].is_a?(Class)
my_each do |i|
return false unless i.is_a?(arg[0])
end
else
my_each do |i|
return false unless i == arg[0]
end
end
else
my_each do |i|
return false unless i
end
end
true
end
def my_any?(*arg)
if block_given?
my_each do |i|
return true if yield i
end
elsif arg[0]
if arg[0].is_a?(Regexp)
my_each do |i|
return true if arg[0].match?(i)
end
elsif arg[0].is_a?(Class)
my_each do |i|
return true if i.is_a?(arg[0])
end
else
my_each do |i|
return true if i == arg[0]
end
end
else
my_each do |i|
return true if i
end
end
false
end
def my_none?(*arg)
if block_given?
my_each do |i|
return false if yield i
end
elsif arg[0]
if arg[0].is_a?(Regexp)
my_each do |i|
return false if arg[0].match?(i)
end
elsif arg[0].is_a?(Class)
my_each do |i|
return false if i.is_a?(arg[0])
end
else
my_each do |i|
return false if i == arg[0]
end
end
else
my_each do |i|
return false if i
end
end
true
end
def my_count(*arg)
counter = 0
if block_given?
my_each do |i|
counter += 1 if yield i
end
elsif arg[0]
if arg[0] == Regexp
my_each do |i|
counter += 1 if arg[0].match?(i)
end
elsif arg[0].is_a?(Class)
my_each do |i|
counter += 1 if i.is_a?(arg[0])
end
else
my_each do |i|
counter += 1 if i == arg[0]
end
end
else
my_each do |i|
counter += 1 if i
end
end
counter
end
def my_map(*arg)
map_val = []
if block_given? && arg[0].is_a?(Proc)
my_each do |i|
map_val << arg[0].call(i)
end
elsif block_given?
my_each do |i|
map_val << (yield i)
end
elsif !block_given? && !arg[0]
return enum_for
elsif arg[0].is_a?(Proc)
my_each do |i|
map_val << arg[0].call(i)
end
else
my_each do |i|
map_val << i if i
end
end
map_val
end
def my_inject(*arg)
if block_given? && arg[0]
result = arg[0]
for i in (0..to_a.length - 1) do
result = (yield result, to_a[i])
end
elsif block_given?
result = to_a[0]
for i in (1..to_a.length - 1) do
result = (yield result, to_a[i])
end
elsif arg[0] && arg[1]
result = arg[0]
for i in (0..to_a.length - 1) do
result = result.send(arg[1], to_a[i])
end
elsif arg[0]
result = to_a[0]
for i in (1..to_a.length - 1) do
result = result.send(arg[0], to_a[i])
end
elsif !block_given?
yield
end
result
end
end
def multiply_els(*arg)
arr = arg[0].to_a
arr.my_inject(:*)
end
# rubocop:enable Style/For
# rubocop:enable Metrics/ModuleLength
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity