Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method · added in Ruby 3.2
Class#attached_object
attached_object → object
object.c
View on GitHub ↗
VALUE
rb_class_attached_object(VALUE klass)
{
if (!FL_TEST(klass, FL_SINGLETON)) {
rb_raise(rb_eTypeError, "`%"PRIsVALUE"' is not a singleton class", klass);
}
return RCLASS_ATTACHED_OBJECT(klass);
}
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
-
3.2
-
Class#attached_object, which returns the object for which the receiver is the singleton class. RaisesTypeErrorif the receiver is not a singleton class. [Feature #12084]Exampleclass 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
-