[ruby-core:113942] [Ruby master Feature#19521] Support for `Module#name=` and `Class#name=`.
From:
"ufuk (Ufuk Kayserilioglu) via ruby-core" <ruby-core@...>
Date:
2023-06-19 15:34:11 UTC
List:
ruby-core #113942
Issue #19521 has been updated by ufuk (Ufuk Kayserilioglu).
ioquatix (Samuel Williams) wrote in #note-25:
> And in any case, your example is already trivially possible without this PR/change:
>
> ```
> c = Class.new
> String = c
> c.new.foo # => undefined method `foo' for #<String:0x00000001308896c8> (NoMethodError)
> ```
There is a big difference with what's possible today and what would be possible after this change:
**Curently**:
```ruby
c = Class.new
String = c
# both of the following show basically the same error, since `String` now means something else.
c.new.upcase # => undefined method `upcase` for #<String:0x00000001308896c8> (NoMethodError)
String.new.upcase # => undefined method `upcase` for #<String:0x00000001308896ca> (NoMethodError)
```
**After this change**:
```ruby
c = Class.new
c.set_temporary_name "String"
# this is now a confusing error
c.new.upcase # => undefined method `upcase` for #<String:0x00000001308896c8> (NoMethodError)
# since this is not an error at all
String.new.upcase # => ""
```
In my opinion, it is very confusing to refer to something as `"String"` when actually referring to the `String` constant gives you different behaviour.
I believe this exactly what @eregon is trying to prevent, and I am in agreement. The current "temporary names" are things like `"<Module:0x00005606bc180560>"` which are clearly not references to constants, due to the naming, and any other temporary should also be explicitly non-constant names to prevent any confusion like the above.
----------------------------------------
Feature #19521: Support for `Module#name=` and `Class#name=`.
https://bugs.ruby-lang.org/issues/19521#change-103596
* 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/7483) introduces `Module#name=` (and thus also `Class#name=`) to set the temporary class name. The name assignment has no effect if the module/class already has a permanent name.
```ruby
c = Class.new do
self.name = "fake name"
end
c = Class.new
c.name = "fake name"
```
Alternatively, we could use `set_name`:
```ruby
Class.new do
set_name "fake_name"
end
```
Setting the name of a class changes its current name, irrespective of whether it's been assigned a permanent name, or has nested modules/classes which have cached a previous name. We might like to limit the cases where a name is set, e.g. only once, only if the name is nil, or only if it's not already permanent. There is no real harm in any of those options, just inconsistency.
## 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 do
self.name = name
class_eval(&block) if block
end
end
def labeled_class(name, superclass = Object, &block)
Class.new(superclass) do
self.name = name
class_eval(&block) if block
end
end
module_function :labeled_class
```
Because the name cannot be set as part of `.new`, we have to have a separate block to set the name, before calling `class_eval`. I think the ergonomics and performance of this are slightly worse than the [counter proposal](https://bugs.ruby-lang.org/issues/19520).
--
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/