[英]alias_method_chain and monkey patching
我想修補為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
的最后一個實現?
第一個alias_method_chain
執行alias_method :foo, :foo_with_flag
(第一個定義的副本),當您重新定義foo_with_flag
:foo
仍然是第一個定義的別名。 您必須在第二個定義(不是“整個鏈”)之后再次執行alias_method :foo, :foo_with_flag
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.