From: "ioquatix (Samuel Williams) via ruby-core" Date: 2023-02-25T12:02:30+00:00 Subject: [ruby-core:112603] [Ruby master Feature#19450] Is there an official way to set a class name without setting a constant? Issue #19450 has been updated by ioquatix (Samuel Williams). Here are some of the places in my own code I'd like to use such a feature: - https://github.com/ioquatix/bake/blob/a571f0c47cc202a4b46a836b87b7383d84f74fa0/lib/bake/scope.rb#L30 - "Scope" - https://github.com/ioquatix/bake/blob/a571f0c47cc202a4b46a836b87b7383d84f74fa0/lib/bake/base.rb#L32 - "" probably makes sense. - https://github.com/socketry/utopia/blob/7f24d503f46000076fbdeeb6e28e293c9544b37b/lib/utopia/controller.rb#LL65C29-L65C29 - probably "Controller" - https://github.com/ioquatix/sus/blob/65e408bad260d168701bad79b2cb905ef65435e8/lib/sus/base.rb#L43 - probably "Base" - https://github.com/ioquatix/sus/blob/65e408bad260d168701bad79b2cb905ef65435e8/lib/sus/it.rb#L11 - probably "It" There are other systems that would benefit from this, e.g. RSpec, or any test framework which loads tests into anonymous modules, or any system that basically creates `Module.new`/`Class.new` and subsequently loads code from one or more files into them. > But it doesn't affect Module#name and error messages, which IMO is good, so error messages don't lie about this. I disagree, the anonymous `#<#::Bar:0x00007fd0ad81ec70>` names are near useless. Where did it come from? Where was it defined? > Related, Marshal and other places use rb_mod_name() (or similar) to find the class name. In my PR, it's still considered an anonymous class. So it can't be marshalled. > 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. ---------------------------------------- Feature #19450: Is there an official way to set a class name without setting a constant? https://bugs.ruby-lang.org/issues/19450#change-102050 * 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/