[ruby-core:112655] [Ruby master Feature#19450] Is there an official way to set a class name without setting a constant?
From:
"ioquatix (Samuel Williams) via ruby-core" <ruby-core@...>
Date:
2023-03-02 04:25:17 UTC
List:
ruby-core #112655
Issue #19450 has been updated by ioquatix (Samuel Williams).
> I haven't come across a case where you cannot also define name on those nested classes/modules, though it is possible such a case exists.
It's not reasonable, and in fact cumbersome, to expect users to override `Class#name` for every class defined in an anonymous namespace. However, as an alternative/addition to this proposal, having `Class#name` invoke `superclass.name` rather than using the internally defined class path would probably fix some of the shortcomings of overriding `Class#name`.
> Apologies, it but was not obvious to me where you demonstrated this. Could you link to where you think you already demonstrated this?
https://bugs.ruby-lang.org/issues/19450#note-19
> Are you of the opinion that the need to assign a name to an anonymous module or class without assigning it to a constant is more common than 1%?
Yes, based on my own usage and analysis of other projects on GitHub. A lot of code uses assignment to unique constant names, but this itself is overly complex. The reason for doing that is to give it a meaningful name, not because a constant in `Object` is desired. A common example of this is RSpec: https://github.com/rspec/rspec-core/blob/d722da4a175f0347e4be1ba16c0eb763de48f07c/lib/rspec/core/example_group.rb#L842-L848 but this type of usage is fairly common.
> The decision of whether to add a keyword argument to an existing method or instead add an additional method is always a judgement call.
I understand. My initial proposal was to use a keyword argument, but the implementation was more complex as you suggest, so I opted for an optional positional argument.
I would say, the keyword argument is slightly more ergonomic, but at the cost of implementation complexity and perhaps a little bit of performance.
I will leave it to the developer meeting to decide what interface makes the most sense. I also appreciate the use of methods like `labeled_class` and `labeled_module` but those don't compose well long term, i.e. if there are other optional arguments, you'd end up with a lot of messy methods. So, I'd personally vote for keyword arguments or optional positional arguments.
----------------------------------------
Feature #19450: Is there an official way to set a class name without setting a constant?
https://bugs.ruby-lang.org/issues/19450#change-102103
* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
----------------------------------------
This is the best I could come up with:
```ruby
klass = Class.new
Object.const_set("Klass", klass)
Object.send(:remove_const, "Klass")
puts klass.new
# => #<Klass:0x0000000100a9d688>
```
Can we do better?
What about something like:
```ruby
Class.new(name: "Klass")
```
or
```ruby
Class.new do
def self.name
"Klass"
end
end
```
etc
--
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/