From: "ufuk (Ufuk Kayserilioglu)" Date: 2021-11-29T21:29:07+00:00 Subject: [ruby-core:106318] [Ruby master Feature#12084] `Class#instance` Issue #12084 has been updated by ufuk (Ufuk Kayserilioglu). Agreed that `instance` is a bad name for this concept and we should not be basing the name on the `Singleton` class, since it is not the same kind of singleton in the `singleton_class`. From what I understand this would be exposing the concept of "attached_object" to user code and that concept is [clearly documented inside the CRuby source code](https://github.com/ruby/ruby/blob/cb4e2cb55a59833fc4f1f6db2f3082d1ffcafc80/include/ruby/internal/module.h#L36) to mean exactly this: ``` ... * - attached object: A singleton class knows its unique instance. * The instance is called the attached object for the singleton class. ... ``` So a good name would be `attached_object` and its behaviour could be: ```ruby Array.singleton_class.attached_object # => Array "foo".singleton_class.attached_object # => "foo" Array.attached_object # => nil "foo".attached_object # => nil ``` ---------------------------------------- Feature #12084: `Class#instance` https://bugs.ruby-lang.org/issues/12084#change-94945 * 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: