-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_each.rb
61 lines (41 loc) · 888 Bytes
/
array_each.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
a = %w[The Quick Brown Fox Jumps]
class Array
def my_each(&block)
i = 0
loop do
break if i >= size
block.call(self[i])
i += 1
end
end
def my_map(&block)
result = []
my_each do |e|
result << block.call(e)
end
result
end
def my_reject(&block)
my_select { |e| !block.call(e) }
end
def my_select(&block)
result = []
my_each { |e| result << e if block.call(e) }
result
end
end
a.each { |element| p element }
puts "x" * 25
a.my_each { |element| p element }
puts "x" * 25
p a.map { |element| "#{element}!" }
puts "x" * 25
p a.my_map { |element| "#{element}!" }
puts "x" * 25
p a.select { |element| element == "The" }
puts "x" * 25
p a.my_select { |element| element == "The" }
puts "x" * 25
p a.reject { |element| element == "The" }
puts "x" * 25
p a.my_reject { |element| element == "The" }