Permalink
Browse files

add the implementation of Object#singleton_class

  • Loading branch information...
1 parent a1087cc commit 0635b6d6e3c6efc8859b85438cf465cdb5539e1f @Watson1978 Watson1978 committed Aug 19, 2012
Showing with 25 additions and 0 deletions.
  1. +25 −0 object.c
View
@@ -198,6 +198,30 @@ rb_obj_class(VALUE obj)
return rb_class_real(CLASS_OF(obj), true);
}
+/*
+ * call-seq:
+ * obj.singleton_class -> class
+ *
+ * Returns the singleton class of <i>obj</i>. This method creates
+ * a new singleton class if <i>obj</i> does not have it.
+ *
+ * If <i>obj</i> is <code>nil</code>, <code>true</code>, or
+ * <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
+ * respectively.
+ * If <i>obj</i> is a Fixnum or a Symbol, it raises a TypeError.
+ *
+ * Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
+ * String.singleton_class #=> #<Class:String>
+ * nil.singleton_class #=> NilClass
+ */
+
+static VALUE
+rb_obj_singleton_class(VALUE obj, SEL sel)
+{
+ return rb_singleton_class(obj);
+}
+
+
static void
init_copy(VALUE dest, VALUE obj)
{
@@ -3076,6 +3100,7 @@ Init_Object(void)
rb_objc_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
rb_objc_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
+ rb_objc_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
rb_objc_define_method(rb_cNSObject, "clone", rb_obj_clone_imp, 0);
rb_objc_define_method(rb_cNSObject, "dup", rb_nsobj_dup, 0);
rb_objc_define_method(rb_cNSObject, "__type__", rb_obj_type, 0);

0 comments on commit 0635b6d

Please sign in to comment.