Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method · added in Ruby 3.2

Class#attached_object

attached_object → object

Returns the object for which the receiver is the singleton class.

Raises an TypeError if the class is not a singleton class.

Example
class Foo; end

Foo.singleton_class.attached_object        #=> Foo
Foo.attached_object                        #=> TypeError: `Foo' is not a singleton class
Foo.new.singleton_class.attached_object    #=> #<Foo:0x000000010491a370>
TrueClass.attached_object                  #=> TypeError: `TrueClass' is not a singleton class
NilClass.attached_object                   #=> TypeError: `NilClass' is not a singleton class

Ruby version changes

  1. 3.2
    • Class#attached_object, which returns the object for which the receiver is the singleton class. Raises TypeError if the receiver is not a singleton class. [Feature #12084]

      Example
      class Foo; end
      
      Foo.singleton_class.attached_object        #=> Foo
      Foo.new.singleton_class.attached_object    #=> #<Foo:0x000000010491a370>
      Foo.attached_object                        #=> TypeError: `Foo' is not a singleton class
      nil.singleton_class.attached_object        #=> TypeError: `NilClass' is not a singleton class
      
See also #allocate #inherited .new #json_creatable? also in 4.1 · 4.0 · 3.4 · 3.3