[前][次][番号順一覧][スレッド一覧]

ruby-changes:15144

From: shugo <ko1@a...>
Date: Tue, 23 Mar 2010 18:39:12 +0900 (JST)
Subject: [ruby-changes:15144] Ruby:r27022 (trunk): * object.c (rb_obj_singleton_class): new method

shugo	2010-03-23 18:38:54 +0900 (Tue, 23 Mar 2010)

  New Revision: 27022

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=27022

  Log:
    * object.c (rb_obj_singleton_class): new method
      Kernel#singleton_class.  [ruby-core:21702]

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_object.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 27021)
+++ ChangeLog	(revision 27022)
@@ -1,3 +1,8 @@
+Tue Mar 23 18:35:46 2010  Shugo Maeda  <shugo@r...>
+
+	* object.c (rb_obj_singleton_class): new method
+	  Kernel#singleton_class.  [ruby-core:21702]
+
 Tue Mar 23 01:13:59 2010  Tanaka Akira  <akr@f...>
 
 	* ext/socket: use rsock_ prefix for internal initialization functions.
Index: object.c
===================================================================
--- object.c	(revision 27021)
+++ object.c	(revision 27022)
@@ -162,6 +162,29 @@
     return rb_class_real(CLASS_OF(obj));
 }
 
+/*
+ *  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)
+{
+    return rb_singleton_class(obj);
+}
+
 static void
 init_copy(VALUE dest, VALUE obj)
 {
@@ -2585,6 +2608,7 @@
     rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
 
     rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
+    rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
     rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
     rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
     rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 27021)
+++ test/ruby/test_object.rb	(revision 27022)
@@ -561,4 +561,25 @@
       end
     end.call
   end
+
+  def test_singleton_class
+    x = Object.new
+    xs = class << x; self; end
+    assert_equal(xs, x.singleton_class)
+
+    y = Object.new
+    ys = y.singleton_class
+    assert_equal(class << y; self; end, ys)
+
+    assert_equal(NilClass, nil.singleton_class)
+    assert_equal(TrueClass, true.singleton_class)
+    assert_equal(FalseClass, false.singleton_class)
+
+    assert_raise(TypeError) do
+      123.singleton_class
+    end
+    assert_raise(TypeError) do
+      :foo.singleton_class
+    end
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]