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

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


I think the best way is:

def thank name  yield name if block_given?end


def thank(arg, &block)  yield argendproc = Proc.new do|name|   puts "Thank you #{name}"end

Then you can do:

thank("God", &proc)


a different way to what Nada proposed (it's the same, just different syntax):

proc = Proc.new do |name|    puts "thank you #{name}"enddef thank(proc_argument, name)    proc_argument.call(name)endthank(proc, "for the music") #=> "thank you for the music"thank(proc, "for the songs you're singing") #=> "thank you for the songs you're singing"

It works, but I don't like it. Nevertheless it will help readers understand HOW procs and blocks are used.