I want to define a method in ruby using
define_method
within another function. Example code is below.
def demo(method_name)
variable = 5
define_method "#{method_name}" do
#stuff
end
end
Inside the newly defined method I want to be able to access the variable:
variable=5
that was previously defined. For example I want to be able to do :
define_method "#{method_name}" do
return variable*variable
end
and get variable squared.
I want to be able to:
demo("squared")
x = squared # => 25
Is there a way I can pass the variable "variable" into the define_method even though it is not in the same scope?
do #stuff endis in the same scope asvariable = 5. Even I am wrong, that block has access to a mother's scope*. Check this:a = 42; l =-> do; b = 22; l2 = -> do; puts a; end; end; l.call.call;*I am not sure about proper title so I made up that.