ruby-changes:24190
From: nobu <ko1@a...>
Date: Thu, 28 Jun 2012 06:12:57 +0900 (JST)
Subject: [ruby-changes:24190] nobu:r36241 (trunk): prepend: fix ancestors order
nobu 2012-06-28 06:12:46 +0900 (Thu, 28 Jun 2012) New Revision: 36241 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36241 Log: prepend: fix ancestors order * class.c (rb_mod_ancestors): fix ancestors order. [ruby-core:45919][Bug #6658] [ruby-dev:45861][Bug #6659] Modified files: trunk/ChangeLog trunk/class.c trunk/test/ruby/test_module.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 36240) +++ ChangeLog (revision 36241) @@ -1,3 +1,8 @@ +Thu Jun 28 06:12:42 2012 Nobuyoshi Nakada <nobu@r...> + + * class.c (rb_mod_ancestors): fix ancestors order. + [ruby-core:45919][Bug #6658] [ruby-dev:45861][Bug #6659] + Wed Jun 27 21:28:59 2012 Kazuhiro NISHIYAMA <zn@m...> * lib/racc/parser.rb: NotImplementError is not exist. Index: class.c =================================================================== --- class.c (revision 36240) +++ class.c (revision 36241) @@ -838,11 +838,22 @@ rb_mod_ancestors(VALUE mod) { VALUE p, ary = rb_ary_new(); + VALUE origin = RCLASS_ORIGIN(mod); - for (p = mod; p; p = RCLASS_SUPER(p)) { + p = mod; + if (origin == mod) { + origin = 0; + } + else { + p = RCLASS_SUPER(p); + } + for (; p; p = RCLASS_SUPER(p)) { if (FL_TEST(p, FL_SINGLETON)) continue; - if (BUILTIN_TYPE(p) == T_ICLASS) { + if (p == origin) { + rb_ary_push(ary, mod); + } + else if (BUILTIN_TYPE(p) == T_ICLASS) { rb_ary_push(ary, RBASIC(p)->klass); } else { Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 36240) +++ test/ruby/test_module.rb (revision 36241) @@ -1273,9 +1273,9 @@ def test_prepend_inheritance bug6654 = '[ruby-core:45914]' - a = Module.new - b = Module.new {include a} - c = Class.new {prepend b} + a = labeled_module("a") + b = labeled_module("b") {include a} + c = labeled_class("c") {prepend b} assert_operator(c, :<, b, bug6654) assert_operator(c, :<, a, bug6654) end @@ -1299,4 +1299,32 @@ end end end + + def test_prepend_class_ancestors + bug6658 = '[ruby-core:45919]' + m = labeled_module("m") + c = labeled_class("c") {prepend m} + assert_equal([m, c], c.ancestors[0, 2], bug6658) + end + + def test_prepend_module_ancestors + bug6659 = '[ruby-dev:45861]' + m0 = labeled_module("m0") + m1 = labeled_module("m1") {prepend m0} + assert_equal([m0, m1], m1.ancestors, bug6659) + end + + def labeled_module(name, &block) + Module.new do + singleton_class.class_eval {define_method(:to_s) {name}} + class_eval(&block) if block + end + end + + def labeled_class(name, superclass = Object, &block) + Class.new(superclass) do + singleton_class.class_eval {define_method(:to_s) {name}} + class_eval(&block) if block + end + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/