generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_enumerable_methods.rb
108 lines (98 loc) · 2.26 KB
/
my_enumerable_methods.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
module Enumerable
def my_each
counter = 0
arr = Array.new(counter)
if block_given?
while counter < size
yield(arr[counter] = to_a[counter])
counter += 1
end
arr
else
to_enum(:each)
end
end
def my_each_with_index
if block_given?
(0...size).my_each do |counter|
yield self[counter], counter
end
else
to_enum(:my_each_with_index)
end
end
def my_select
if block_given?
select_in = []
my_each do |counter|
select_in << counter if yield(counter)
end
select_in
else
to_enum(:select)
end
end
def my_all?(args = nil)
element = true
if args.nil?
block_given? ? my_each { |x| element = false unless yield x } : element = my_all? { |x| !x.nil? && (x != false) }
else
my_each { |x| element = false unless args == x }
end
element
end
def my_any?(args = nil)
val = false
if args.nil?
block_given? ? my_each { |x| val = true if yield x } : val = my_any? { |x| !x.nil? && (x != false) }
else
my_each { |x| val = true if args == x }
end
val
end
def my_none?(args = nil)
val = true
if args.nil?
block_given? ? my_each { |x| val = false if yield x } : my_any? { |x| return false if x == true }
else
my_each { |x| val = false if args == x }
end
val
end
def my_count(item = nil)
count = 0
if item.nil?
block_given? ? my_each { |x| count += 1 if yield x } : my_each { count += 1 }
else
my_each { |x| count += 1 if item == x }
end
count
end
def my_map
return to_enum unless block_given?
arr = []
my_each { |x| arr << yield(x) }
arr
end
def my_inject(val1 = nil, val2 = nil)
first_element = true
case val1
when nil then val = to_a[0]
when Numeric, Symbol
val = val1
first_element = false
end
if block_given?
my_each { |x| first_element == true ? first_element = false : val = yield(val, x) }
else
val1, val2 = val2, val1 if val1.is_a? Symbol
val = my_inject(val1) do |total, x|
instance_eval "#{total} #{val2} #{x}", __FILE__, __LINE__
end
end
val
end
end
def multiply_els(arr)
arr.inject { |product, n| product * n }
end