-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydef.rb
52 lines (49 loc) · 857 Bytes
/
mydef.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
puts 'Method for map'
def My_map(arr)
result = Array.new
arr.each do |element|
temp=yield(element)
result.push(temp)
end
result
end
arr = [1,2,3,4,5]
puts "result is #{My_map(arr) { |num| num.even? }}"
puts 'Method for map!'
def My_mapbang(arr)
i=0
arr.each do |element|
arr[i]=yield(element)
i=i+1
end
arr
end
arr = [1,2,3,4,5]
puts "result is #{My_mapbang(arr) { |num| num*2 }}"
puts arr
puts 'Method for my_select'
def my_select(arr)
result = Array.new
arr.each do |element|
if(yield(element))
result.push(element)
end
end
result
end
arr = [1,2,3,4,5]
puts my_select(arr) { |num| num.even? }
puts 'Method for map'
def My_selectbang(arr)
i=0
arr.each do |element|
if(!(yield(element)))
arr.slice!(i)
end
i=i+1
end
arr
end
arr = [1,2,3,4,5]
puts "result is #{My_selectbang(arr) { |num| num.even? }}"
puts arr