如何从 Rails 中的 gem 覆盖类的方法? [英] How to override a method of a class from a gem in Rails?
本文介绍了如何从 Rails 中的 gem 覆盖类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要覆盖一个 gem 类的 find
方法的行为.
I need to override the behavior of the find
method of a class from a gem.
这是 gem 中的代码:
This is the code in the gem:
module Youtube
class Display
attr_accessor :base
def find(id, options = {})
detailed = convert_to_number(options.delete(:detailed))
options[:detailed] = detailed unless detailed.nil?
base.send :get, "/get_youtube", options.merge(:youtube_id => id)
end
end
end
如何在我自己的 Rails 应用程序的 YoutubeSearch 控制器中覆盖上述 find
方法?
How do I override the above find
method in my own YoutubeSearch Controller of my Rails Application?
def find(id, options = {})
//Code here
end
推荐答案
在 config/initializers
目录中创建一个 .rb 文件,代码如下:
Create a .rb file in config/initializers
directory with the following code:
Youtube::Display.class_eval do
def find(id, options = {})
# Code here
end
end
这篇关于如何从 Rails 中的 gem 覆盖类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文