Reputation: 2821
s = Proc.new {|x|x*2}
puts "proc:" + (s.call(5)).to_s
def foo(&a)
a.call(5)
end
foo{|x| puts "foo:" + (x*3).to_s}
Running this program produces the output:
proc:10
foo:15
How does the value 3 from the foo block get passed to the proc? I expected this output:
proc:10
foo:10
The proc is always called with the value 5 as the argument because foo is defined as:
a.call(5)
Why is foo 15 in the output?
Upvotes: 0
Views: 95
Reputation: 246847
The value 3 does not get passed to the proc because you're not passing s
to foo
. You probably meant to write
foo {|x| puts "foo: #{s.call(x)}"}
or
puts "foo: #{foo(&s)}"
Additionally, these are equivalent:
def foo_1(x, &a)
puts a.call(x)
end
def foo_2(x)
puts yield(x)
end
foo_1(5, &s) #=> 10
foo_2(5, &s) #=> 10
Upvotes: 4
Reputation: 370172
Because the block outputs x*3
(as opposed to s which returns x*2
) and 5*3
is 15.
Upvotes: 4