From: "jemmai (Jemma Issroff)" Date: 2021-12-14T20:27:40+00:00 Subject: [ruby-core:106679] [Ruby master Feature#12084] `Class#instance` Issue #12084 has been updated by jemmai (Jemma Issroff). matz (Yukihiro Matsumoto) wrote in #note-6: > But there's still no real-world use-case. We have defined the method described [here](https://github.com/panorama-ed/memo_wise/blob/b20b2cad20f9186c915f2b4a315ef3cfcc0a323a/lib/memo_wise/internal_api.rb#L147-L176) , for MemoWise, a memoization gem. (For what it���s worth, we named it `original_class_from_singleton`.) It���s feasible that someone wants to memoize a method on a singleton class. Here is an example*: ``` require "memo_wise" class << String prepend MemoWise def example_method "example" end memo_wise :example_method end String.example_method # => ���example��� ``` 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. We therefore resolve the original class by searching `ObjectSpace`for the class whose singleton class is the one we received: ``` def self.original_class_from_singleton(klass) ObjectSpace.each_object(Module).find do |cls| cls.singleton_class == klass end end ``` I believe this is almost exactly the built in method being proposed. *It is worth noting that this same functionality could be achieved by the following snippet, instead of opening up the singleton class: ``` require "memo_wise" class String prepend MemoWise def self.example_method "example" end memo_wise self: :example_method end String.example_method # => "example" ``` But I think part of the beauty of Ruby is that there are multiple ways to express the same sentiment, and while we allow for opening up the singleton class to do this, it makes sense to me that a gem like MemoWise must support this case, and therefore a method as defined in this issue does have a real world use case. It���s also worth noting that a different design decision in MemoWise might make the need go away soon for MemoWise specifically. ---------------------------------------- Feature #12084: `Class#instance` https://bugs.ruby-lang.org/issues/12084#change-95354 * 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: