Reputation: 13811
Consider this code:
x = 10
10.times{
x++
}
assert binding.getVariable("x") == 20
As far, I'm concerned this is what happening in the above code: Script
class has a reference to the variable x
which has value 10
and it calls the times
closure which will access the variable x
from Script
. So basically the scoping of x
is been done by Script
class.
But I wonder when binding
came into the picture? In sense, I haven't called new Binding()
or anything, but still its there.
I'm bit confused with it. What is the exact use of binding
? Does Script
class creates binding
, if so that is how it manages scoping of x
within times
method?
Thanks in advance.
Upvotes: 1
Views: 587
Reputation: 171114
The script class constructs a new binding when it is constructed
You can see it does it in the constructor of Script
Upvotes: 2