summaryrefslogtreecommitdiff
path: root/class.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-13 05:50:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-13 05:50:38 +0000
commit609c7420d77aed3492648b6dae7adf118d3a8ab9 (patch)
tree49c7334638da00e14c044070b1727fef327af379 /class.c
parent111c791c3730608a0a961ab86d153c824e26db9f (diff)
class.c: rb_singleton_class_get
* class.c (rb_singleton_class_get): get the singleton class if exists, or nil. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40682 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'class.c')
-rw-r--r--class.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/class.c b/class.c
index 2035d90..a37a153 100644
--- a/class.c
+++ b/class.c
@@ -1448,6 +1448,26 @@ singleton_class_of(VALUE obj)
return klass;
}
+/*!
+ * Returns the singleton class of \a obj, or nil if obj is not a
+ * singleton object.
+ *
+ * \param obj an arbitrary object.
+ * \return the singleton class or nil.
+ */
+VALUE
+rb_singleton_class_get(VALUE obj)
+{
+ VALUE klass;
+
+ if (SPECIAL_CONST_P(obj)) {
+ return rb_special_singleton_class(obj);
+ }
+ klass = RBASIC(obj)->klass;
+ if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
+ if (rb_ivar_get(klass, id_attached) != obj) return Qnil;
+ return klass;
+}
/*!
* Returns the singleton class of \a obj. Creates it if necessary.