How to pass parameters to a proc when calling it by a method?

Croplio picture Croplio · Aug 4, 2010 · Viewed 13.2k times · Source
proc = Proc.new do |name|
  puts "Thank you #{name}!"
end
def thank
  yield
end

proc.call # output nothing, just fine
proc.call('God') # => Thank you God!

thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank &proc.call('God') # Error!
thank proc.call('God') # Error!
# So, what should I do if I have to pass the 'God' to the proc and use the 'thank' method at the same time ?

Thanks :)

Answer

Daniel O'Hara picture Daniel O'Hara · Aug 4, 2010

I think the best way is:

def thank name
  yield name if block_given?
end