Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 326
Croplio Avatar asked Aug 04 '10 15:08

Croplio


People also ask

How do you pass a parameter to a procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

How do you pass parameter values by reference in procedure parameters?

By default, the EXECUTE PROCEDURE statement passes parameters to a database procedure by value. To pass a value by reference, use the BYREF option. If a parameter is passed by reference, the called database procedure can change the contents of the variable, and the change is visible to the calling program.

How do you pass a parameter to a stored procedure while executing?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

Can we pass parameters to stored procedures?

You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.


1 Answers

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

proc = Proc.new do |name|
    puts "thank you #{name}"
end

def thank(proc_argument, name)
    proc_argument.call(name)
end

thank(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.

like image 145
BenKoshy Avatar answered Sep 29 '22 10:09

BenKoshy