tags:

views:

74

answers:

2
s = Proc.new {|x|x*2}

def one_arg(x)
  puts yield(x)
end

one_arg(5, &s)

How does one_arg know about &s?

+3  A: 

The & operator turns the Proc into a block, so it becomes a one-argument method with a block (which is called with yield). If you had left off the & so that it passed the Proc directly, you would have gotten an error.

Chuck
+3  A: 

By doing the &s, you're telling one_arg that you'd like your Proc s passed as a block (please correct me if I'm wrong). An equivalent writing would be

one_arg(5) do |x|
  x *2
end

There have been a few questions here on SO as of late that deal with this. August Lilleaas has a pretty nice write up about some of the intricacies of all this Ruby madness.

theIV