Can you explain this?
I want to eval values and calculations from two different sources. One source gives me the following info(programmatically):
'a = 2'
The second source gives me this expression to evaluate:
'a + 3'
This works:
a = 2
eval 'a + 3'
This also works:
eval 'a = 2; a + 3'
But what I really need is this, and it doesn’t work:
eval 'a = 2'
eval 'a + 3'
I would like to understand the difference, and how can I make the last option work.
Thanks for your help.
You could create a
Binding, and associate the same binding with eachevalcall:This way any variables that you create in earlier
evalcalls are available later on (as long as you use the same binding object).Instead of using
Kernel::eval, you could useBinding#eval, which would make the association clearer: