From: "Eregon (Benoit Daloze) via ruby-core" Date: 2023-04-27T15:36:03+00:00 Subject: [ruby-core:113362] [Ruby master Feature#19520] Support for `Module.new(name)` and `Class.new(superclass, name)`. Issue #19520 has been updated by Eregon (Benoit Daloze). Dan0042 (Daniel DeLorme) wrote in #note-35: > But let's say it really is too slow to do a `rb_funcall` for each nested namespace; Module#name is cached (and returns a frozen string) and should likely remain cached. But you are not suggesting to change Module#name anyway. However on a case such as a NoMethodError, there is no notion of parent namespace. We would just call `inspect` on the class/module. It is the responsibility of that class/module `inspect` to show the parent namespaces, like the default Module#inspect does. So I don't see any performance issue here, besides having to call `inspect` on the Module (if overridden) for NoMethodError#message. That is indeed a potential concern, see #18285, but it seems less problematic than `object.inspect` which was done previously. ---------------------------------------- Feature #19520: Support for `Module.new(name)` and `Class.new(superclass, name)`. https://bugs.ruby-lang.org/issues/19520#change-102925 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- See 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/ ______________________________________________ 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/