2012-11-19 134 views
1

我想猴補丁爲alias_method_chain創建了一個方法,但該方法覆蓋不叫alias_method_chain和猴子修補

# foo.rb 
require 'active_support/core_ext' 

class Foo 
    def foo 
    "original foo" 
    end 

    def foo_with_flag 
    "foo with flag" 
    end 

    alias_method_chain :foo, :flag 
end 

# foo_ext.rb 
class Foo 
    def foo_with_flag 
    "overridden foo with flag" 
    end 
end 

foo = Foo.new 
foo.foo # => "foo with flag" 
foo.foo_with_flag # => "overridden foo with flag" 

我怎樣才能讓Foo#foo使用最後執行的Foo#foo_with_flag

回答

2

第一alias_method_chainalias_method :foo, :foo_with_flag(一副本的第一個定義的),當你重新定義foo_with_flag:foo仍然alias'ed到的第一個定義。你必須在第二個定義(不是「整個鏈」)後再次執行alias_method :foo, :foo_with_flag