Working on some functionality where I need to be able to define methods and local vars from strings via a binding
I think the code speaks best for itself, so here is what I'm trying to accomplish:
mybind = binding
mybind.eval("a = 5")
mybind.eval("a") #=> 5
a #=> NameError: undefined local variable or method `a'...
# Calling it in global/main scope doesn't find anything, but it's found in the `mybind` scope
mybind.eval("def thing; 3; end")
mybind.eval("thing") #=> 3
thing #=> NameError: undefined local variable or method `thing'...
# Calling it in global/main scope doesn't find anything, but it's found in the `mybind` scope
Essentially, I don't want any methods/vars defined to go to the global scope, but I want them to be callable within a second call to the same binding/eval. These will come through as strings, so callable blocks is out of the question as well.
I've tried instance_eval, class_eval, eval, and instance_exec
instance_eval is ALMOST perfect, except for some reason any local vars defined cannot be accessed within the next instance_eval called.
I'm a bit new to using binding, so if there is something obvious I'm missing, please let me know. Otherwise if there is anybody that has any idea on how to accomplish this, even with a different flow I would really appreciate any help.
Thanks!
I'm on Ruby 2.7.2, but can upgrade if that will provide a solution.