From: "Eregon (Benoit Daloze)" Date: 2021-12-14T20:52:26+00:00 Subject: [ruby-core:106681] [Ruby master Feature#12084] `Class#instance` Issue #12084 has been updated by Eregon (Benoit Daloze). jemmai (Jemma Issroff) wrote in #note-8: > Within MemoWise, we receive the `memo_wise(:example_method)` call on the singleton class of `String`, and must resolve it back to define the memoization on the `String` class itself, not its singleton class. That's not clear to me, why do you need to access String? Can't the memoization be stored on the singleton class (String.singleton_class)? Modules/classes are always "unique", there are no two same instances, so it seems always fine to store the data on the class/module holding the method (the module/class for which `method_defined?(name)` is true/has it in `instance_methods`). > # * Performance concern: searches all Class objects > # But, only runs at load time and results are memoized BTW, it (internally) iterates all objects on most Ruby implementations, not just modules/classes, the filtering is typically done on top because there is no "next instance pointer" (e.g., unlike in some Smalltalk). `ObjectSpace.each_object` is a clear no-no when anyone cares about the time to load a library/use it during startup (it's `O(nb of objects in heap at the time of call)`). ---------------------------------------- Feature #12084: `Class#instance` https://bugs.ruby-lang.org/issues/12084#change-95357 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal ---------------------------------------- For meta-programming/debugging purposes, I would like to request the inverse of `Object#singleton_class`. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the `Singleton` module in the standard library http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html assigns the method name `instance` to such classes, I think `Class#instance` should be the name for such feature. ~~~RUBY Array.singleton_class.instance # => Array "foo".singleton_class.instance # => "foo" ~~~ When the receiver is a class but is not a singleton class, then it should raise an error. ~~~RUBY Array.instance # => error ~~~ -- https://bugs.ruby-lang.org/ Unsubscribe: