This file need to be exactly required after require 'restclient'
Adding load 'lib/restclient/request.rb'
did the trick
Overriding third-party method in Ruby
-
31-08-2022 - |
Question
I'm developing some tool using pure ruby and RestClient and I'd like to override default log_request method of Request class.
lib/restclient/request.rb
module RestClient
class Request
def log_request
RestClient.log << "SECRET"
end
end
end
But now, if I try to test this, it is not working:
$ 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"
Expected to see only SECRET
as output.
I'm probably missing, how to "inject" my code in default RestClient library ?
How can I do this from another file in lib/mytool/somefile.rb ?
Solution 2
OTHER TIPS
Since you place it in your lib folder, this file is never being loaded as RestClient::Request constant is already defined. Place this code inside your config/initializers folder
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow