-
Notifications
You must be signed in to change notification settings - Fork 2
/
enumerables.rb
141 lines (126 loc) · 3.16 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
# rubocop:disable Style/For
# rubocop:disable Metrics/ModuleLength
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
module Enumerable
def my_each
return enum_for unless block_given?
for index in self
yield index
end
end
def my_each_with_index
return enum_for unless block_given?
b = 0
for a in self
yield a, b
b += 1
end
end
def my_select
return enum_for unless block_given?
array = []
my_each do |i|
array.push(i) if yield i
end
array
end
def my_all?(args = nil)
to_a
if block_given?
my_each { |i| return false unless yield(i) == true }
elsif args.nil?
my_each { |i| return false if i == false || i.nil? }
elsif args.instance_of?(Class)
my_each { |i| return false if i.class.superclass != args && i.class != args }
elsif args.instance_of?(Regexp)
my_each { |i| return false unless args.match(i) }
else
my_each { |i| return false if i != args }
end
true
end
def my_any?(args = nil)
to_a
if block_given?
my_each { |i| return true if yield i }
elsif args.nil?
my_each { |i| return true if i }
elsif args.instance_of?(Class)
my_each { |i| return true if i.instance_of?(args) || i.class.superclass == args }
elsif args.instance_of?(Regexp)
my_each { |i| return true if args.match(i) }
else
my_each { |i| return true if i == args }
end
false
end
def my_none?(args = nil)
if block_given?
my_each { |i| return false if yield i }
elsif args.instance_of?(Regexp)
my_each { |i| return false if args.match(i) }
elsif args.instance_of?(Class)
my_each { |i| return false if i.is_a?(args) }
elsif args.nil?
my_each { |i| return false if i }
else
my_each { |i| return false if i == args }
end
true
end
def my_count(*args)
i = 0
if block_given?
my_each do |x|
i += 1 if yield x
end
elsif args.empty?
my_each do |_x|
i += 1
end
else
my_each do |x|
i += 1 if x == args[0]
end
end
i
end
def my_map(arg = nil)
return enum_for unless block_given?
array = []
if arg.nil? && block_given?
my_each { |index| array.push(yield index) }
else
result = arg
my_each { |index| array.push(result.call(index)) }
end
array
end
def my_inject(num = nil, sym = nil)
if sym.instance_of?(Symbol) || sym.instance_of?(String)
result = num
my_each do |item|
result = result.nil? ? item : result.send(sym, item)
end
result
elsif num.instance_of?(Symbol) || num.instance_of?(String)
result = nil
my_each do |item|
result = result.nil? ? item : result.send(num, item)
end
result
else
result = num
my_each do |item|
result = result.nil? ? item : yield(result, item)
end
end
result
end
end
# rubocop:enable Style/For
# rubocop:enable Metrics/ModuleLength
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def multiply_els(array)
array.my_inject(1) { |index, result| result * index }
end