Hi
I thought variable a is set in the context of b ?
Why does this not work? (Shouldn't it work?)
b=binding
b.eval("a=1")
puts a # does not exist
thank you
Opti
Hi
I thought variable a is set in the context of b ?
Why does this not work? (Shouldn't it work?)
b=binding
b.eval("a=1")
puts a # does not exist
thank you
Opti
a=nil
b=binding
b.eval("a=1")
puts a
When does a local variable become accessible? [1]
Actually, the question may be better asked as: “at what point does
Ruby work out that something is a variable?” The problem arises
because the simple expression a could be either a variable or a call to
a method with no parameters. To decide which is the case, Ruby looks for
assignment statements. If at some point in the source prior to the use
of a it sees it being assigned to, it decides to parse a as a variable,
otherwise it treats it as a method.
This issue with variables is not normally a problem. If you do bump into
it, try putting an assignment such as a = nil before the first access to
the variable. This has the additional benefit of speeding up the access
time to local variables that subsequently appear in loops.
On 1/12/22, Die Optimisten <inform@die-optimisten.net> wrote:
Why does this not work? (Shouldn't it work?)
b=binding
b.eval("a=1")
puts a # does not exist