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

ruby-changes:30376

From: nobu <ko1@a...>
Date: Thu, 8 Aug 2013 23:01:33 +0900 (JST)
Subject: [ruby-changes:30376] nobu:r42449 (trunk): object.c: Module#singleton_class?

nobu	2013-08-08 23:01:23 +0900 (Thu, 08 Aug 2013)

  New Revision: 42449

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

  Log:
    object.c: Module#singleton_class?
    
    * object.c (rb_mod_singleton_p): new method Module#singleton_class? to
      return whether the receiver is a singleton class or not.
      [ruby-core:51087] [Feature #7609]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/object.c
    trunk/test/ruby/test_class.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42448)
+++ ChangeLog	(revision 42449)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Aug  8 23:01:20 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* object.c (rb_mod_singleton_p): new method Module#singleton_class? to
+	  return whether the receiver is a singleton class or not.
+	  [ruby-core:51087] [Feature #7609]
+
 Thu Aug  8 21:56:44 2013  Tanaka Akira  <akr@f...>
 
 	* time.c (time_overflow_p): Avoid signed integer overflow.
Index: object.c
===================================================================
--- object.c	(revision 42448)
+++ object.c	(revision 42449)
@@ -2385,6 +2385,14 @@ rb_mod_cvar_defined(VALUE obj, VALUE iv) https://github.com/ruby/ruby/blob/trunk/object.c#L2385
     return rb_cvar_defined(obj, id);
 }
 
+static VALUE
+rb_mod_singleton_p(VALUE klass)
+{
+    if (RB_TYPE_P(klass, T_CLASS) && FL_TEST(klass, FL_SINGLETON))
+	return Qtrue;
+    return Qfalse;
+}
+
 static struct conv_method_tbl {
     const char *method;
     ID id;
@@ -3225,6 +3233,7 @@ Init_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L3233
     rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
     rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */
     rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */
+    rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
 
     rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
     rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
Index: NEWS
===================================================================
--- NEWS	(revision 42448)
+++ NEWS	(revision 42449)
@@ -41,6 +41,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L41
   * New methods:
     * Module#using, which activates refinements of the specified module only
       in the current class or module definition.
+    * Module#singleton_class? returns true if the receiver is a singleton class
+      or false if it is an ordinary class or module.
   * extended methods:
     * Module#refine is no longer experimental.
 
Index: test/ruby/test_class.rb
===================================================================
--- test/ruby/test_class.rb	(revision 42448)
+++ test/ruby/test_class.rb	(revision 42449)
@@ -346,4 +346,10 @@ class TestClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_class.rb#L346
     assert_empty(added.grep(->(k) {c == k[0]}), bug5283)
     assert_equal(:foo, d.foo)
   end
+
+  def test_singleton_class_p
+    feature7609 = '[ruby-core:51087] [Feature #7609]'
+    assert_predicate(self.singleton_class, :singleton_class?, feature7609)
+    assert_not_predicate(self.class, :singleton_class?, feature7609)
+  end
 end

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

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