-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerables.rb
190 lines (179 loc) · 3.5 KB
/
enumerables.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
module Enumerables
def my_each(block = nil)
i = 0
if block_given?
length.times do
if !block.nil?
block.call(self[i])
else
yield(self[i])
end
i += 1
end
else
self
end
end
def my_each_with_index(block = nil)
i = 0
if block_given?
length.times do
if !block.nil?
block.call(self[i], i)
else
yield(self[i], i)
end
i += 1
end
else
self
end
end
def my_select(block = nil)
i = 0
if block_given?
length.times do
if !block.nil?
puts self[i] if block.call(self[i])
elsif yield(self[i])
puts self[i]
end
i += 1
end
else
my_each
end
end
def my_all?(match = nil)
i = 0
statement = true
if block_given?
length.times do
statement = false unless yield(self[i])
i += 1
end
elsif !match.nil?
length.times do
begin
statement = false if self[i].is_a?(match)
rescue StandardError
statement = false if self[i].scan(match)
end
end
end
statement
end
def my_none?(match = nil)
i = 0
statement = false
statement = true if length.zero?
if block_given?
length.times do
statement = true unless yield(self[i])
i += 1
end
elsif !match.nil?
length.times do
begin
statement = true if self[i].is_a?(match)
rescue StandardError
statement = true if self[i].scan(match)
end
i += 1
end
else
length.times do
return true if self[i] == true or self[i].nil?
i += 1
end
end
statement
end
def my_any?(match = nil)
i = 0
statement = false
if block_given?
length.times do
return true if yield(self[i])
i += 1
end
elsif !match.nil?
length.times do
begin
statement = true if self[i].is_a?(match)
rescue StandardError
statement = true if self[i].scan(match)
end
end
else
length.times do
return true if self[i] != false or !self[i].nil?
end
end
statement
end
def my_count(match = nil)
i = 0
if block_given?
c = 0
length.times do
c += 1 if yield(self[i])
i += 1
end
c
elsif !match.nil?
c = 0
length.times do
c += 1 if self[i] == match
i += 1
end
c
else
length.times do
i += 1
end
i
end
end
def my_map(block = nil)
i = 0
new_array = []
my_array = []
my_array = if respond_to?(:to_ary)
self
else
to_a
end
if !block.nil?
my_array.length.times do
new_array.push(block.call(my_array[i]))
i += 1
end
else
my_array.length.times do
new_array.push(yield(my_array[i]))
i += 1
end
end
new_array
end
def my_inject(block = nil)
my_array = if respond_to?(:to_ary)
self
else
to_a
end
if !block.nil?
my_array.my_inject { |sum, x| sum + x }
elsif block_given?
accumulator = my_array[0]
my_array.my_each_with_index do |n, i|
accumulator = yield(accumulator, n) if i != 0
end
accumulator
end
end
end
def multiply_els(array)
array.my_inject { |sum, n| sum * n }
end