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

ruby-changes:27576

From: marcandre <ko1@a...>
Date: Fri, 8 Mar 2013 02:55:01 +0900 (JST)
Subject: [ruby-changes:27576] marcandRe: r39628 (trunk): * class.c (rb_mod_ancestors): Include singleton_class in ancestors list

marcandre	2013-03-08 02:54:49 +0900 (Fri, 08 Mar 2013)

  New Revision: 39628

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

  Log:
    * class.c (rb_mod_ancestors): Include singleton_class in ancestors list
      [Feature #8035]
    
    * test/ruby/test_module.rb (class): test for above
    
    * test/ruby/marshaltestlib.rb (module): adapt test
    
    * NEWS: list change

  Modified files:
    trunk/NEWS
    trunk/class.c
    trunk/test/ruby/marshaltestlib.rb
    trunk/test/ruby/test_module.rb

Index: class.c
===================================================================
--- class.c	(revision 39627)
+++ class.c	(revision 39628)
@@ -909,8 +909,6 @@ rb_mod_ancestors(VALUE mod) https://github.com/ruby/ruby/blob/trunk/class.c#L909
     VALUE p, ary = rb_ary_new();
 
     for (p = mod; p; p = RCLASS_SUPER(p)) {
-	if (FL_TEST(p, FL_SINGLETON))
-	    continue;
 	if (BUILTIN_TYPE(p) == T_ICLASS) {
 	    rb_ary_push(ary, RBASIC(p)->klass);
 	}
Index: NEWS
===================================================================
--- NEWS	(revision 39627)
+++ NEWS	(revision 39628)
@@ -14,6 +14,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L14
 === Language changes
 === Core classes updates (outstanding ones only)
 === Core classes compatibility issues (excluding feature bug fixes)
+
+* Module#ancestors
+
+  The ancestors of a singleton class now include that singleton class.
+
 === Stdlib updates (outstanding ones only)
 === Stdlib compatibility issues (excluding feature bug fixes)
 === C API updates
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 39627)
+++ test/ruby/test_module.rb	(revision 39628)
@@ -1663,6 +1663,20 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1663
     end
   end
 
+  def test_singleton_class_ancestors
+    feature8035 = '[ruby-core:53171]'
+    obj = Object.new
+    assert_equal [obj.singleton_class, Object], obj.singleton_class.ancestors.first(2), feature8035
+
+    mod = Module.new
+    obj.extend mod
+    assert_equal [obj.singleton_class, mod, Object], obj.singleton_class.ancestors.first(3)
+
+    obj = Object.new
+    obj.singleton_class.send :prepend, mod
+    assert_equal [mod, obj.singleton_class, Object], obj.singleton_class.ancestors.first(3)
+  end
+
   private
 
   def assert_top_method_is_private(method)
Index: test/ruby/marshaltestlib.rb
===================================================================
--- test/ruby/marshaltestlib.rb	(revision 39627)
+++ test/ruby/marshaltestlib.rb	(revision 39628)
@@ -40,6 +40,14 @@ module MarshalTestLib https://github.com/ruby/ruby/blob/trunk/test/ruby/marshaltestlib.rb#L40
     end
   end
 
+  def marshal_equal_with_ancestry(o1, msg = nil)
+    marshal_equal(o1, msg) do |o|
+      ancestry = o.singleton_class.ancestors
+      ancestry[ancestry.index(o.singleton_class)] = :singleton_class
+      ancestry
+    end
+  end
+
   class MyObject; def initialize(v) @v = v end; attr_reader :v; end
   def test_object
     o1 = Object.new
@@ -54,25 +62,17 @@ module MarshalTestLib https://github.com/ruby/ruby/blob/trunk/test/ruby/marshaltestlib.rb#L62
   def test_object_extend
     o1 = Object.new
     o1.extend(Mod1)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
     o1.extend(Mod2)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
   end
 
   def test_object_subclass_extend
     o1 = MyObject.new(2)
     o1.extend(Mod1)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
     o1.extend(Mod2)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
   end
 
   class MyArray < Array
@@ -141,25 +141,17 @@ module MarshalTestLib https://github.com/ruby/ruby/blob/trunk/test/ruby/marshaltestlib.rb#L141
   def test_hash_extend
     o1 = Hash.new
     o1.extend(Mod1)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
     o1.extend(Mod2)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
   end
 
   def test_hash_subclass_extend
     o1 = MyHash.new(2)
     o1.extend(Mod1)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
     o1.extend(Mod2)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
   end
 
   def test_bignum
@@ -289,13 +281,9 @@ module MarshalTestLib https://github.com/ruby/ruby/blob/trunk/test/ruby/marshaltestlib.rb#L281
   def test_struct_subclass_extend
     o1 = MyStruct.new
     o1.extend(Mod1)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
     o1.extend(Mod2)
-    marshal_equal(o1) { |o|
-      (class << self; self; end).ancestors
-    }
+    marshal_equal_with_ancestry(o1)
   end
 
   def test_symbol
@@ -404,7 +392,7 @@ module MarshalTestLib https://github.com/ruby/ruby/blob/trunk/test/ruby/marshaltestlib.rb#L392
     o = Object.new
     o.extend Mod1
     o.extend Mod2
-    marshal_equal(o) {|obj| class << obj; ancestors end}
+    marshal_equal_with_ancestry(o)
     o = Object.new
     o.extend Module.new
     assert_raise(TypeError) { marshaltest(o) }
@@ -417,7 +405,7 @@ module MarshalTestLib https://github.com/ruby/ruby/blob/trunk/test/ruby/marshaltestlib.rb#L405
     o = ""
     o.extend Mod1
     o.extend Mod2
-    marshal_equal(o) {|obj| class << obj; ancestors end}
+    marshal_equal_with_ancestry(o)
     o = ""
     o.extend Module.new
     assert_raise(TypeError) { marshaltest(o) }

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

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