Feature #21365
openAdd `Namespace#eval`
Description
I would like a way to eval code on to a Namespace
object. Could we add an eval method that doesn't take a binding object? Writing a new file every time I want to test Namespaces is too cumbersome.
Thanks!
Updated by matheusrich (Matheus Richard) 2 days ago
I'm curious why eval
and not instance_eval
?
Updated by Eregon (Benoit Daloze) 2 days ago
Yep, agreed it'd be far more convenient for testing Namespace.
matheusrich (Matheus Richard) wrote in #note-1:
I'm curious why
eval
and notinstance_eval
?
Why instance_eval
? The distinction between eval/class_eval/instance_eval does not seem relevant here.
Though of course the semantics should be the same as writing the code to a file + Namespace#require
so e.g. ns.eval "def foo = 42"
defines method foo
on ns' copy of Object methods.
Updated by matheusrich (Matheus Richard) 2 days ago
@Eregon (Benoit Daloze) I feel like that's more consistent with the rest of the language. I can't remember if any object as a eval
method. But evaluating some code in a particular instance context makes me think of instance_eval
.
Updated by ufuk (Ufuk Kayserilioglu) 2 days ago
matheusrich (Matheus Richard) wrote in #note-3:
@Eregon (Benoit Daloze) I feel like that's more consistent with the rest of the language. I can't remember if any object as a
eval
method. But evaluating some code in a particular instance context makes me think ofinstance_eval
.
I am not sure I understand. All objects have an eval
method in the form of Kernel#eval
: https://ruby-doc.org/3.4.1/Kernel.html#method-i-eval
Updated by matheusrich (Matheus Richard) 2 days ago
@ufuk (Ufuk Kayserilioglu) fair point. Although it is a private method, so we're never doing "foo".eval(bar)
.