Ruby 1.8 Bug?

像这样一段代码:

require 'rubygems'require 'active_support/all'module A  def f    puts 'this is a'  endendmodule B  def f    puts 'this is b'    super  endendclass C  include A  include Bendputs '** test 1, no hacking'C.new.fmodule A  def f_with_test_a    puts 'in HackingA'    f_without_test_a    puts 'out HackingA'  end  alias_method_chain :f, :test_aendputs '** test 2, hacking A'C.new.fmodule B  def f_with_test_b    puts 'in HackingB'    f_without_test_b    puts 'out HackingB'  end  alias_method_chain :f, :test_bendputs '** test 3, hacking both A & B'C.new.f

这段代码测试Ruby在两层alias_method下的情况,这里创建了两个module,并且用一个类去include它们,测试就是对两个module的同名方法分别做alias_method的hack,看看是否确实能执行正常。

Ruby 1.9中执行结果:

** test 1, no hackingthis is bthis is a** test 2, hacking Athis is bin HackingAthis is aout HackingA** test 3, hacking both A & Bin HackingBthis is bin HackingAthis is aout HackingAout HackingB

可以看到结果是正确的

而Ruby 1.8下却是:

** test 1, no hackingthis is bthis is a** test 2, hacking Athis is bin HackingAthis is aout HackingA** test 3, hacking both A & Bin HackingBthis is b1.rb:13:in `f_without_test_b': super: no superclass method `f' for # (NoMethodError)    from 1.rb:41:in `f'    from 1.rb:49

可以看到在module B执行到super的时候找不到祖先的f方法。。。

Ruby 1.8 Bug?

相关文章:

你感兴趣的文章:

标签云: