In one of Ruby examples I see the following code:
require 'net/http'
req = Net::HTTP::Get.new( "http://localhost:8080/" )
req.basic_auth( "user", "password" )
What is the easiest way to know what Ruby class actually implements this basic_auth method or is it dynamically generated? I have checked public_methods of Net::HTTP::Get and it’s definitely not there. But how to check what class actually implements it?
Generally, you would use the
Kernel#methodmethod to get theMethodobject for the method in question and then you would use theMethod#ownermethod to ask theMethodobject where it was defined.So,
should answer your question.
Except, in this particular case, that won’t work because
reqis aNet::HTTP::Getobject andNet::HTTP::Getoverrides themethodmethod to mean something completely different. In particular, it doesn’t take an argument, thus the above code will actually raise anArgumentError.However, since
Net::HTTP::Getinherits fromObjectandObjectmixes inKernel, it is legal to bind theKernel#methodmethod to an instance ofNet::HTTP::Get:So, there’s your answer: the method is defined in
Net::HTTPHeader.