我正在開發一些使用純ruby和RestClient的工具,我想覆蓋Request類的默認log_request方法。在Ruby中重寫第三方方法
的lib/RESTClient實現/ request.rb
module RestClient
class Request
def log_request
RestClient.log << "SECRET"
end
end
end
但現在,如果我嘗試測試,它無法正常工作:
$ irb
irb(main):001:0> require 'restclient'
=> true
irb(main):002:0> RestClient.log = "stdout"
=> "stdout"
irb(main):003:0> RestClient.get("http://localhost")
RestClient.get "http://localhost", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"
預計只看到SECRET
作爲輸出。
我可能會缺少,如何「注入」我的代碼在默認RestClient庫?
如何從lib/mytool/somefile.rb中的其他文件執行此操作?
的問題是,猴子補丁沒有裝到精確要求。你需要在某個地方需要request.rb。 – Pafjo
我發現,'require'restclient/request''還不夠,因爲它已經被'require'restclient''遇到了。正確的解決方案是'load'lib/restclient/request.rb'' – Vestel