From: "ioquatix (Samuel Williams) via ruby-core" Date: 2023-02-20T02:33:16+00:00 Subject: [ruby-core:112499] [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). Okay, so I tried it out, but it doesn't work consistently: ```ruby def labeled_class(name, superclass = Object, &block) Class.new(superclass) do singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s} class_eval(&block) if block end end foo = labeled_class("foo") foo.class_eval("class Bar; end") p foo p foo::Bar ``` ---------------------------------------- Feature #19450: Is there an official way to set a class name without setting a constant? https://bugs.ruby-lang.org/issues/19450#change-101943 * 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/