-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerable-methods.rb
146 lines (128 loc) · 2.62 KB
/
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
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
module Enumerable
def my_each
if block_given?
for i in self
yield i
end
else
self.to_enum
end
end
def my_each_with_index
if block_given?
# add index to pass it to block
index = 0
for i in self
yield [i, index]
index += 1
end
else
self.to_enum
end
end
def my_select
if block_given?
# create array for selected value
ary = []
self.my_each { |e|
ary << e if (yield e) == true
}
ary
else
self.to_enum
end
end
def my_all?
flag_bool = true
for e in self
if e.nil? || e == false
flag_bool = false
break
elsif block_given?
flag_bool = false if (yield e) == false
end
end
flag_bool
end
def my_any?
flag_bool = true
for e in self
if block_given?
flag_bool = false if e.nil? || e == false
(yield e) == true ? flag_bool = true : flag_bool = false
end
end
flag_bool
end
def my_none?
flag_bool = true
for e in self
if block_given?
if e.nil? || e == false || ((yield e) == false)
flag_bool = true
else
flag_bool = false
break
end
else
if e == true
flag_bool = false
break
else
flag_bool = true
end
end
end
flag_bool
end
def my_count (*args)
# If a block is given, counts the number of elements for which the block returns a true value
if block_given?
arr_with_block = []
self.my_each { |e| arr_with_block << e if (yield e) == true }
arr_with_block.size
# Returns the number of elements if no args and no block
elsif args.empty?
self.length
# If an argument is given, counts the number of elements
else
arr_with_arg = []
self.my_each { |e| arr_with_arg << e if e == args[0] }
arr_with_arg.size
end
end
def my_map(&block)
if block_given?
mapped_arr = []
self.my_each { |e| mapped_arr << block.call(e) }
mapped_arr
else
self.to_enum
end
end
def my_inject (*args)
# p self.methods.sort
arr = self.to_a
if block_given?
if args.empty?
acc = arr.first
for e in arr[1,arr.size]
acc = yield(acc, e)
end
else
acc = args[0]
for e in arr
acc = yield(acc, e)
end
end
# args.empty? ? acc = self.first : acc = args[0]
else
self.to_enum
end
acc
end
end
def multiply_els(arr)
arr.my_inject {|acc, e| acc * e}
end
pr = proc {|i| i*i }