[ruby-core:122242] [Ruby Feature#21365] Add `Namespace#eval`

Issue #21365 has been reported by tenderlovemaking (Aaron Patterson). ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by matheusrich (Matheus Richard). I'm curious why `eval` and not `instance_eval`? ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113395 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by Eregon (Benoit Daloze). Yep, agreed it'd be far more convenient for testing Namespace. matheusrich (Matheus Richard) wrote in #note-1:
I'm curious why `eval` and not `instance_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. ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113396 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by matheusrich (Matheus Richard). @Eregon 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`. ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113399 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by ufuk (Ufuk Kayserilioglu). matheusrich (Matheus Richard) wrote in #note-3:
@Eregon 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`.
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 ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113400 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by matheusrich (Matheus Richard). @ufuk fair point. Although it is a private method, so we're never doing `"foo".eval(bar)`. ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113403 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by Eregon (Benoit Daloze). `ns.eval "self"` would be the Namespace "main" object, not the `Namespace` instance itself, so `instance_eval` would be a bad fit here. AFAIK, `instance_eval` is only called as such because any `def foo; end` inside it will definite a singleton method on the instance. And similarly `class_eval` defines `def foo; end` as a class instance method. This is not relevant here, `def foo; end` would have normal top-level `def` semantics (in that namespace). ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113438 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by fxn (Xavier Noria). `instance_eval` modifies the nesting in the eval'ed code. Since namespaces are modules, `instance_eval` already works: ```ruby ns = Namespace.new ns.instance_eval "p Module.nesting" ``` That prints `[Namespace]`. I believe @tenderlove's wants a regular top-level execution context, where the nesting would be empty, in particular (mod https://bugs.ruby-lang.org/issues/21318). ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113441 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by matheusrich (Matheus Richard). Thanks for the clarification, everyone! ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113442 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by fxn (Xavier Noria). I want to clarify that I explained that `instance_eval` works, and what it does, because `Namespace` is a `Module`. But that does not mean I believe that is good. Namespaces try to be one thing, but by being modules they carry other things that, in my view, are not quite coherent with the feature. It is another instance of my hunch that namespaces should be a new entity with new rules. In this particular case, I believe they should not respond to `instance_eval`, or `module_eval` or anything. Precisely, the proposed `eval` in this ticket is what makes sense to me: "run this code in your execution context". ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113450 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by matz (Yukihiro Matsumoto). Namespace.eval accepted. Matz. ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113601 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/

Issue #21365 has been updated by tenderlovemaking (Aaron Patterson). I sent a PR [here](https://github.com/ruby/ruby/pull/13701). :-) ---------------------------------------- Feature #21365: Add `Namespace#eval` https://bugs.ruby-lang.org/issues/21365#change-113837 * Author: tenderlovemaking (Aaron Patterson) * Status: Open ---------------------------------------- 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! -- https://bugs.ruby-lang.org/
participants (6)
-
Eregon (Benoit Daloze)
-
fxn (Xavier Noria)
-
matheusrich (Matheus Richard)
-
matz (Yukihiro Matsumoto)
-
tenderlovemaking (Aaron Patterson)
-
ufuk (Ufuk Kayserilioglu)