-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying (non-permanent) module and class names. #7376
Conversation
I don't think it's a good idea to modify |
Why not? I don't think this is something that should be a new method, because it is not something that should be invoked more than once, and should be set before any internal callbacks are invoked for the class/module. |
Because it is almost never needed. It also requires you modify two methods instead of a single method.
What callbacks are you referring to? For In the places where you plan to use this feature, is there a reason it couldn't be a separate method? If so, can you provide an example? |
The class name is cached, if you define child classes in a callback, e.g. class Base
def self.inherited(derived)
nested = derived.const_set(:Nested, Class.new)
nested.name # force full class name to generated
end
end
class Child1 < Base
end
pp Child1 # => Child1
pp Child1::Nested # Child1::Nested
child2 = Class.new(Base, "Child2")
pp child2 # => Child2
pp child2::Nested # => #<Class:0x00007f1dd9e23850>::Nested I'm against introducing a method for this feature as it's not something that should be set after the class is created and the semantics of setting it afterwards are confusing at best. Just to be super clear, here is the expected behaviour (and the one given by this PR): child2 = Class.new(Base, "Child2")
pp child2 # => Child2
pp child2::Nested # => Child2::Nested |
Assuming you wanted this working in |
There are over 100 usage of The current implementations in I'm tired of seeing things like:
This proposal makes it much nicer:
Finally, it's unfair to say "so rarely used" since up until the point where this interface is introduced, it's impossible to do such a thing correctly anyway. It would only be apparent some years after introduction whether it's "so rarely used" in practice. And evidence given by As one more data point, searching for ruby set anonymous class name reveals that this is not an uncommon request and there are various strategies, none of which is completely correct and/or flexible (i.e. using an arbitrary string as a class name). |
I think this needs discussion in a dev meeting first. |
The problem is obvious, isn't it? |
Ah and BTW ruby/spec doesn't use any |
- `Class.new(superclass, name)` - `Module.new(name)` such that #name returns the given name UNTIL it's assigned to a constant.
Since envutil.rb is used by many default gems which may support earlier versions, it should not use the syntax only available on the head. |
We have decided not to go ahead with this approach at this time. |
Enables:
Class.new(superclass, name)
Module.new(name)
such that #name returns the given name UNTIL it's assigned to a constant.
https://bugs.ruby-lang.org/issues/19450