[ruby-core:112848] [Ruby master Feature#19520] Support for `Module.new(name)` and `Class.new(superclass, name)`.
From:
"ufuk (Ufuk Kayserilioglu) via ruby-core" <ruby-core@...>
Date:
2023-03-10 16:23:55 UTC
List:
ruby-core #112848
Issue #19520 has been updated by ufuk (Ufuk Kayserilioglu).
ioquatix (Samuel Williams) wrote in #note-12:
> I don't understand this argument at all. The assumptions are already broken by the trivial examples already given.
Let me try to explain: I think we are focusing too much on the broken case and not enough on the non-broken cases. Under normal circumstances constants tell you the names they are bound to when asked via the `Module#name` method. Certain classes/modules override that to change what is displayed, but one can always get the name that Ruby knows them by through the `Module.instance_method(:name).bind_call(mod)` call.
If we allow what is returned by `Module#name` to be any arbitrary string that the user chooses (for example, the original request had examples of `Module(/foo/bar/baz.rb)` or similar), then there won't be a way to ever get the actual name of that module, regardless of it was anonymous or named. It would forever return the name that the user specified. I think this is the part of the proposal that @eregon and me are particularly against, that it would break the current meaning and operation of `Module#name`, regardless of if the semantics of what is returned by it today is broken in some cases or not.
Again, I understand the need to give better display names for `Module`s, I have needed to use that myself at various points. That's why I am suggesting to maybe think about implementing this in a way that doesn't mess with the current `Module#name` method. I hope this makes the argument a little bit more clear.
----------------------------------------
Feature #19520: Support for `Module.new(name)` and `Class.new(superclass, name)`.
https://bugs.ruby-lang.org/issues/19520#change-102360
* 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/