From: "Eregon (Benoit Daloze) via ruby-core" Date: 2023-02-25T12:41:13+00:00 Subject: [ruby-core:112605] [Ruby master Feature#19450] Is there an official way to set a class name without setting a constant? Issue #19450 has been updated by Eregon (Benoit Daloze). ioquatix (Samuel Williams) wrote in #note-18: > > One requirement for that is the given name should not be a valid constant name then (not start with a uppercase letter). > > That's not how it works internally, there is actually a flag for anonymous/permanent name. It's a requirement for humans to always be able to tell the difference. That flag is not exposed or shown. > I wondered about whether your point of class name consistent was valid, but I found cases like this: Yes, as I already mentioned in https://github.com/ruby/ruby/pull/7376#issuecomment-1445057920. But `remove_const` or overriding a constant is very rarely used, there is a reason they are private or warn. And in practice they aren't used much, so (the original) `Module#name` does reflect in 99+% how to access a named module, and that is valuable. This feature hurts that, and `Module#name` is now possibly a "random user-supplied value" or worse a "looks like a constant path but is fake and was never assigned" (the latter one can be addressed by the requirement above). --- I don't get why you want to show the file path in Module#name, this information is already available in the backtrace and in fact is more precise in the backtrace: ``` Module.new do self::C = Class.new c = self::C.new c.foo end --- t.rb:4:in `block in
': undefined method `foo' for #<#::C:0x00007f73673fa1b0> (NoMethodError) c.foo ^^^^ from t.rb:1:in `initialize' from t.rb:1:in `new' from t.rb:1:in `
' ``` How is `#` more useful? (it might look better to you, but what is the actual usefulness of it?) The relevant information here is `t.rb:4` which is already there. I have the feeling you hate the notation `#<#::C:0x5678>`, which yes that's a bit cryptic, but it's missing the point this is hardly relevant in an exception such as the one above. For the example in https://bugs.ruby-lang.org/issues/19450#note-6 you can just define `inspect` as an instance method of that class and be done with it. You can customize `inspect` on the class itself like Rails does if you think people will do a lot of `p self.class` in `sus` or so. In other words, a module name and a file path are separate things. ---------------------------------------- Feature #19450: Is there an official way to set a class name without setting a constant? https://bugs.ruby-lang.org/issues/19450#change-102052 * 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 # => # ``` 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/