-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.rb
More file actions
205 lines (179 loc) · 4.39 KB
/
enum.rb
File metadata and controls
205 lines (179 loc) · 4.39 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
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, Metrics/ModuleLength, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Style/EvalWithLocation, Security/Eval, Metrics/BlockNesting, Style/GuardClause
module Enumerable
def my_each
return to_enum(:my_each) if block_given? != true
if is_a?(Hash) && Proc.new.arity > 1
for i in 0...length
yield keys[i], values[i]
end
else
for i in self
yield i
end
end
self
end
def my_each_with_index
return to_enum(:my_each_with_index) if block_given? != true
j = 0
my_each do |i|
yield i, j
j += 1
end
self
end
def my_select
return to_enum(:my_select) if block_given? != true
if is_a?(Hash)
filtered = {}
my_each { |key, value| filtered[key] = value if yield key, value }
else
filtered = []
my_each { |i| filtered.push(i) if yield i }
end
filtered
end
def my_all?(*param)
if block_given?
my_each do |i|
return false unless yield i
end
elsif param.empty?
return false if include?(false || nil)
elsif param[0].is_a?(Class)
my_each do |i|
return false unless i.is_a?(param[0])
end
elsif param[0].is_a?(Regexp)
my_each do |i|
return false unless i.match(param[0])
end
else
my_each do |i|
return false unless i.eql? param[0]
end
end
true
end
def my_any?(*param)
if block_given?
my_each do |i|
return true if yield i
end
elsif param.empty?
return true if include?(false || nil)
elsif param[0].is_a?(Class)
my_each do |i|
return true if i.is_a?(param[0])
end
elsif param[0].is_a?(Regexp)
my_each do |i|
return true if i.match(param[0])
end
else
my_each do |i|
return true if i.eql? param[0]
end
end
false
end
def my_none?(*param)
if block_given?
my_each do |i|
return false if yield i
end
elsif param.empty?
return false if include?(true)
elsif param[0].is_a?(Class)
my_each do |i|
return false if i.is_a?(param[0])
end
elsif param[0].is_a?(Regexp)
my_each do |i|
return false if i.match(param[0])
end
else
my_each do |i|
return false if i.eql? param[0]
end
end
true
end
def my_count(param = nil)
counter = 0
j = 0
if param.nil? != true
my_each do |i|
counter += 1 if param == i
end
elsif block_given?
my_each do |i|
counter = j if yield i
j += 1
end
else
my_each do
j += 1
counter = j
end
end
counter
end
def my_map(*param, &block)
collected = []
code_block = Proc.new(&block) if block_given?
code_block = param[0] if param[0].is_a?(Proc)
if code_block.is_a?(Proc)
my_each do |i|
collected.push(code_block.call(i))
end
else
return to_enum(:my_map)
end
collected
end
def my_inject(accum = nil, symb = nil, &block)
object_passed = is_a?(Range) ? to_a : self
object_length = object_passed.length
if block_given?
j = 1
j = 0 unless accum.nil?
accum = first if accum.nil?
if symb.nil?
while j < object_length
accum = block.call(accum, object_passed[j])
j += 1
end
else
symb.is_a?(String) || symb.is_a?(Symbol)
symb = symb.to_s
object_passed.my_each do |i|
accum = eval "#{accum} #{symb} #{i}"
end
end
else
j = 0
j = 1 if symb.nil?
unless accum.nil?
if (accum.is_a?(Symbol) || accum.is_a?(String)) && symb.nil?
symb = accum.to_s
accum = first
while j < object_length
accum = eval "#{accum} #{symb} #{object_passed[j]}"
j += 1
end
elsif accum.is_a?(Integer) && (symb.is_a?(Symbol) || symb.is_a?(String))
while j < object_length
accum = eval "#{accum} #{symb} #{object_passed[j]}"
j += 1
end
end
end
end
accum
end
end
def multiply_els(arr)
arr.my_inject(:*)
end
# rubocop:enable Style/For, Metrics/ModuleLength, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Style/EvalWithLocation, Security/Eval, Metrics/BlockNesting, Style/GuardClause