-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_ampersand.rb
54 lines (42 loc) · 1.13 KB
/
04_ampersand.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
=begin
if object is a block, it converts the block into a simple proc.
if object is a Proc, it converts the object into a block while preserving the lambda? status of the object.
if object is not a Proc, it first calls #to_proc on the object and then converts it into a block.
=end
def sample(number)
print "\n\n"
print "-" * 20
print "sample #{number}"
print "-" * 20
print "\n\n"
end
# if object is a block, it converts the block into a simple proc.
sample(1)
def capturing_block(&block)
block.call
end
capturing_block do
puts "Block Captured"
end
# if object is a Proc, it converts the object into a block while preserving the lambda? status of the object.
sample(2)
x_10 = lambda{ |x| x * 10 }
puts [1,2,3].map &x_10
def preserve(&block)
puts "Am I lambda Proc? #{block.lambda?}"
result = block.call(20)
puts result
end
preserve &x_10
# if object is not a Proc, it first calls #to_proc on the object and then converts it into a block.
sample(3)
class Multiply
def initialize(factor)
@factor = factor
end
def to_proc
proc { |x| @factor * x }
end
end
by_99 = Multiply.new(99)
puts [1,2,3].map(&by_99)