diff options
author | Ufuk Kayserilioglu <[email protected]> | 2022-09-27 01:19:22 +0300 |
---|---|---|
committer | Jean Boussier <[email protected]> | 2022-10-20 17:30:17 +0200 |
commit | 0378e2f4a8319440dd65c82b16f189161472d237 (patch) | |
tree | 6c2c6dd91c624fd4ae6ad4be5dd3a6d04528df37 /class.c | |
parent | 192bc725290ca4b271bff2bae6123d84c25f7173 (diff) |
Add Class#attached_object
Implements [Feature #12084]
Returns the object for which the receiver is the singleton class, or
raises TypeError if the receiver is not a singleton class.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6450
Diffstat (limited to 'class.c')
-rw-r--r-- | class.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -1589,6 +1589,33 @@ rb_class_subclasses(VALUE klass) return class_descendants(klass, true); } +/* + * call-seq: + * 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. + * + * 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 + */ + +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 rb_attr_get(klass, id_attached); +} + static void ins_methods_push(st_data_t name, st_data_t ary) { |