
Issue #19520 has been updated by Dan0042 (Daniel DeLorme). ioquatix (Samuel Williams) wrote in #note-33:
@Dan0042 unfortunately your proposal doesn't really work for nested classes without major performance issues.
I sort of understand what you mean, but I think it's premature to dismiss an idea just based on the fear that it **might** have bad performance. The focus should be on whether the design/API is ok. Then **if** the implementation turns out measurably too slow **and** there is no way to remedy, the idea can be scrapped. But let's say it really is too slow to do a `rb_funcall` for each nested namespace; I can already think of an easy way to mitigate that: only call #to_s if #name is undefined. That way, performance will be unchanged for regular classes assigned to constants. And that's just the first workaround that came to mind; there may even be other ways. ```ruby x = Class.new x.new.inspect #=> #<#<Class:0x00005597d624bd28>:0x00005597d61aa3d8> def x.to_s; "hey"; end x.new.inspect #=> #<hey:0x00005597d6860998> NAMED = x x.new.inspect #=> #<NAMED:0x00005597d6845968> ``` ---------------------------------------- Feature #19520: Support for `Module.new(name)` and `Class.new(superclass, name)`. https://bugs.ruby-lang.org/issues/19520#change-102923 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- See <https://bugs.ruby-lang.org/issues/19450> for previous discussion and motivation. [This proposal](https://github.com/ruby/ruby/pull/7376) introduces the `name` parameter to `Class.new` and `Module.new`: ```ruby Class.new(superclass, name) Module.new(name) ``` As a slight change, we could use keyword arguments instead. ## Example usage The current Ruby test suite has code which shows the usefulness of this new method: ```ruby def labeled_module(name, &block) Module.new do singleton_class.class_eval { define_method(:to_s) {name} alias inspect to_s alias name to_s } class_eval(&block) if block end end module_function :labeled_module def labeled_class(name, superclass = Object, &block) Class.new(superclass) do singleton_class.class_eval { define_method(:to_s) {name} alias inspect to_s alias name to_s } class_eval(&block) if block end end module_function :labeled_class ``` The updated code would look like this: ```ruby def labeled_module(name, &block) Module.new(name, &block) end def labeled_class(name, superclass = Object, &block) Class.new(superclass, name, &block) end module_function :labeled_class ``` -- https://bugs.ruby-lang.org/