/
enumerable.rb
103 lines (93 loc) · 1.4 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
module Enumerable
def my_each
i = 0
while i < self.size
yield( self[i] )
i+=1
end
self
end
def my_each_with_index
i = 0
while i < self.size
yield(self[i], i)
i+=1
end
self
end
def my_select
output = Array.new
self.my_each do |x|
if yield(x)
output << x
end
end
output
end
def my_all?
output = true
self.my_each do |x|
if !yield(x)
output = false
end
end
output
end
def my_any?
output = false
self.my_each do |x|
if yield(x)
output = true
end
end
output
end
def my_none?
output = true
self.my_each do |x|
if yield(x)
output = false
end
end
output
end
def my_count
output = 0
self.my_each do |x|
output += 1
end
output
end
def my_map(a_proc)
@output = Array.new
self.my_each do |x|
@output << a_proc.call(x)
end
if a_proc
block_arr = Array.new
@output.my_each do |x|
block_arr << yield(x)
end
@output = block_arr
end
@output
end
def my_inject(ini=nil)
output = ini if ini
self.my_each do |x|
if output
output = yield(output, x)
else
output = x
end
end
output
end
end
def multiply_els arr
output = 1
arr.my_each do |x|
output *= x
end
output
end