[ruby-core:113374] [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-04-28 10:59:55 UTC
List:
ruby-core #113374
Issue #19520 has been updated by Eregon (Benoit Daloze).
ioquatix (Samuel Williams) wrote in #note-38:
> In any case, this also affects the output of errors such that those names may not be "eval" able. @Eregon can you clarify if this is still a problem for you? If not, why not?
Yes, it is a problem if the output just looks confusing or lies about the name.
For @Dan0042 's example in https://bugs.ruby-lang.org/issues/19520#note-32 `#<MyTemplate["path/to/file"]>:0x000055e017895038>` seems not so confusing, and it is eval-able.
I do wonder though, why create anonymous classes for this though?
Why not `#<MyTemplate:0x000055e017895038 @path="path/to/file">` for an instance of MyTemplate and so no anonymous classes?
IMO creating anonymous classes is too expensive, it's something that makes sense for tests but not much more than that.
Similar to creating tons of singleton classes, that's just bad for performance.
And obviously anonymous classes are anonymous, they are not meant to be easy to refer to.
If you want that, why not name the class? What requires a new anonymous class instead of just using the superclass/a common named class for that use case?
----------------------------------------
Feature #19520: Support for `Module.new(name)` and `Class.new(superclass, name)`.
https://bugs.ruby-lang.org/issues/19520#change-102941
* 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/