[ruby-core:112914] [Ruby master Feature#19520] Support for `Module.new(name)` and `Class.new(superclass, name)`.
From:
"Eregon (Benoit Daloze) via ruby-core" <ruby-core@...>
Date:
2023-03-16 11:48:10 UTC
List:
ruby-core #112914
Issue #19520 has been updated by Eregon (Benoit Daloze).
fxn (Xavier Noria) wrote in #note-26:
> We also know the coupling ends there. These entities ar highly decoupled in Ruby by design. I can have `C = Class.new; c = C; remove_const :C`, and the class in `c` is no longer reachable through the constant after its name. If a Ruby programmer expects that, they have to revise that expectaction because it is just baseless.
I'm afraid you missed the point. Maybe https://bugs.ruby-lang.org/issues/19520#note-17 and https://bugs.ruby-lang.org/issues/19520#note-21 help to make it clearer.
Every Ruby programmer when they see e.g. `undefined method 'zzz' for #<Foo::Bar:0x00007efc38711fc0> (NoMethodError)` expects that `Foo::Bar` in code would refer to the class of that object.
Yes, it's not a guarantee. But it holds in practice 99.99+%. If it doesn't hold then it's a very serious bug for whatever breaks it, just like the cases you mentioned are unsupported with Zeitwerk, here it would be unsupported for developers sanity.
Hence `Module.new(name)` is harmful because it will make people non-consciously break that all the time.
----------------------------------------
Feature #19520: Support for `Module.new(name)` and `Class.new(superclass, name)`.
https://bugs.ruby-lang.org/issues/19520#change-102432
* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
----------------------------------------
See <https://bugs.ruby-lang.org/issues/19450> for previous discussion and motivation.
[This proposal](https://github.com/ruby/ruby/pull/7376) introduces the `name` parameter to `Class.new` and `Module.new`:
```ruby
Class.new(superclass, name)
Module.new(name)
```
As a slight change, we could use keyword arguments instead.
## Example usage
The current Ruby test suite has code which shows the usefulness of this new method:
```ruby
def labeled_module(name, &block)
Module.new do
singleton_class.class_eval {
define_method(:to_s) {name}
alias inspect to_s
alias name to_s
}
class_eval(&block) if block
end
end
module_function :labeled_module
def labeled_class(name, superclass = Object, &block)
Class.new(superclass) do
singleton_class.class_eval {
define_method(:to_s) {name}
alias inspect to_s
alias name to_s
}
class_eval(&block) if block
end
end
module_function :labeled_class
```
The updated code would look like this:
```ruby
def labeled_module(name, &block)
Module.new(name, &block)
end
def labeled_class(name, superclass = Object, &block)
Class.new(superclass, name, &block)
end
module_function :labeled_class
```
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/