Hi. Is there a way to create a lambda that references local variables not yet defined ( with out pawing them to the prod) ?
I.e.
x = lambda {a+b}
a = 1
b = 2
c = x.call
Thank you
Hi. Is there a way to create a lambda that references local variables not yet defined ( with out pawing them to the prod) ?
I.e.
x = lambda {a+b}
a = 1
b = 2
c = x.call
Thank you
List Rb wrote:
Hi. Is there a way to create a lambda that references local variables
not yet defined ( with out pawing them to the prod) ?I.e.
x = lambda {a+b}
a = 1
b = 2
c = x.call
I don't know what "pawing them to the prod" means, but you can do
a, b = nil, nil
x = lambda {a+b}
a = 1
b = 2
c = x.call
This is because Ruby makes a parse-time decision as to whether 'a' is a
local variable or a method call. If no assignment to 'a' has been seen
earlier in the scope, then it is interpreted as a method call instead:
self.a()
Since this is done while the parse tree is built, prior to execution, it
doesn't make any difference whether the assignment is actually executed
or not. For example:
def x
"hello"
end
if false
x = 1
end
puts x # shows 'nil' since x is a local variable
This is necessary to have any sort of efficiency. Otherwise, a simple
statement like "b = a + 1" inside a loop would have to decide at
run-time, every time it is executed, whether 'a' is a local variable or
a method.
--
Posted via http://www.ruby-forum.com/\.