Croplio Croplio - 1 year ago 62
Ruby Question

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

proc = do |name|
puts "Thank you #{name}!"
def thank
end # output nothing, just fine'God') # => Thank you God!

thank &proc # output nothing, too. Fine;
thank &proc('God') # Error!
thank &'God') # Error!
thank'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 Source

I think the best way is:

def thank name
  yield name if block_given?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download