-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
134 lines (121 loc) · 3.38 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
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ModuleLength
module Enumerable
def my_each
return to_enum(:my_each) unless block_given?
i = 0
while i < to_a.length
yield(to_a[i])
i += 1
end
self
end
def my_each_with_index
return to_enum(:my_each_with_index) unless block_given?
i = 0
my_each do |item|
yield(item, i)
i += 1
end
self
end
def my_select
return to_enum(:my_select) unless block_given?
result = []
each do |e|
result << e if yield(e)
end
result
end
def my_all?(data = nil)
if block_given?
my_each { |item| return false if yield(item) == false }
return true
elsif data.nil?
my_each { |n| return false if n.nil? || n == false }
elsif !data.nil? && (data.is_a? Class)
my_each { |n| return false unless n.instance_of?(data) }
elsif !data.nil? && (data.is_a? Regexp)
my_each { |n| return false unless data.match(n) }
else
my_each { |n| return false if n != data }
end
true
end
def my_any?(data = nil)
if block_given?
my_each { |item| return true if yield(item) }
elsif data.nil?
my_each { |n| return true if n.nil? }
elsif data.is_a? Class
my_each { |n| return true if n.instance_of?(data) }
elsif data.is_a? Regexp
my_each { |n| return true if data.match(n) }
else
my_each { |n| return true if n == data }
end
false
end
def my_none?(data = nil)
if block_given?
my_each { |item| return false unless yield(item) == false }
return true
elsif data.nil?
my_each { |n| return false unless n.nil? || n == false }
elsif !data.nil? && (data.is_a? Class)
my_each { |n| return false if n.instance_of?(data) }
elsif !data.nil? && (data.is_a? Regexp)
my_each { |n| return false if data.match(n) }
else
my_each { |n| return false unless n != data }
end
true
end
def my_count(data = nil)
count = 0
if block_given?
my_each { |item| count += 1 if yield(item) }
elsif !data.nil? && (data.is_a? Class)
my_each { |item| count += 1 if item.instance_of?(data) }
elsif !data.nil? && (data.is_a? Regexp)
my_each { |item| count += 1 if data.match(item) }
elsif !data.nil?
my_each { |item| count += 1 if item == data }
else
count = !defined?(length).nil? ? length : size
end
count
end
def my_map(proc1 = nil)
new_array = []
if !proc1.nil? && proc1.respond_to?(:call)
my_each { |item| new_array.push(proc1.call(item)) }
elsif block_given?
my_each { |item| new_array.push(yield(item)) }
elsif !block_given?
return to_enum(:my_map)
else
new_array = self
end
new_array
end
def my_inject(*data)
(raise LocalJumpError if !block_given? && data[0].nil? && data[1].nil?)
if block_given?
net = data[0] ? yield(first, data[0]) : first
drop(1).my_each { |item| net = yield(net, item) }
elsif data[0]
if data[1]
net = data[0]
my_each { |item| net = net.send(data[1], item) }
else
net = first
drop(1).my_each { |item| net = net.send(data[0], item) }
end
end
net
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ModuleLength
def multiply_els(array)
array.my_inject { |sum, item| sum * item }
end