summaryrefslogtreecommitdiff
path: root/class.c
diff options
context:
space:
mode:
authorUfuk Kayserilioglu <ufuk@paralaus.com>2022-09-27 01:19:22 +0300
committerJean Boussier <jean.boussier@gmail.com>2022-10-20 17:30:17 +0200
commit0378e2f4a8319440dd65c82b16f189161472d237 (patch)
tree6c2c6dd91c624fd4ae6ad4be5dd3a6d04528df37 /class.c
parent192bc725290ca4b271bff2bae6123d84c25f7173 (diff)
downloadruby-0378e2f4a8319440dd65c82b16f189161472d237.tar.gz
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.
Diffstat (limited to 'class.c')
-rw-r--r--class.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/class.c b/class.c
index 7a32951e60..b0d23153de 100644
--- a/class.c
+++ b/class.c
@@ -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)
{