我想重写一个 gem 方法(Jekyll 扩展),如下所示:
File: lib/jekyll-amazon/amazon_tag.rb https://github.com/tokzk/jekyll-amazon/blob/8fd9d9ff676e7e1dedd18755e5d0d98300f9e3fc/lib/jekyll-amazon/amazon_tag.rb.
module Jekyll
module Amazon
class AmazonTag < Liquid::Tag
def detail(item)
...
end
end
end
end
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)
我已将具有相同结构的代码放在我的项目中的文件夹 中config/initializers/presentation.rb
罢工>_plugins/presentation.rb
。如果我更改方法的名称detail
到一个新名称,它可以工作,但我无法让它覆盖该名称detail
。
我做错了什么?
(注:在 jekyll-amazon gem 0.2.2 版本中,detail
方法是私有的;我已在本地更改了此设置,以便该方法不再是私有的。)
您可以使用别名方法 https://ruby-doc.org/core-2.2.2/Module.html#method-i-alias_method
module Jekyll
module Amazon
class AmazonTag < Liquid::Tag
alias_method :old_detail, :detail
def detail(item)
# do your stuff here
# eventually pass your stuff to old method
old_detail(item)
end
end
end
end
Liquid::Template.register_tag('amazon', Jekyll::Amazon::AmazonTag)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)