-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enumerable.rb
70 lines (58 loc) · 1.08 KB
/
Enumerable.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
#!/usr/bin/env ruby
module Enumerable
def my_each
for i in self do
yield i
end
end
def my_each_with_index
is=0
for i in self do
yield i, is
is+=1
end
end
def my_select(&block)
self.my_each{ |x| p x if block.call(x) }
end
def exec(data,&block)
@result=[]
data.my_each do |x|
begin
(block.call(x)) ? (@result << true) : (@result << false)
rescue
@result << false
end
end
end
def my_all?(&block)
data=self
exec(data,&block)
(@result.include?(false)) ? (p false) : (p true)
end
def my_any?(&block)
data=self
exec(data,&block)
(@result.include?(true)) ? (p true) : (p false)
end
def my_count(&block)
data=self
result=0
if block_given?
data.my_each do |x|
exec(data,&block)
result+=1 if (@result.include?(true))
end
else
data.my_each do |x|
result+=1
end
end
p result
end
def my_map(&block)
data=self
data.my_each { |i| p block.call(i) }
end
[1,2,3].my_map{|i| i*i}
end