问题
Let's say that I've got a method available in my workspace in Ruby. Is there a command I can use that will recover the fully qualified name of the method?
I'm looking for something similar to the which function in Matlab, that returns an unambiguous name for any argument it's given.
I ask because I'm writing a test in Cucumber that needs to send particular GET and POST requests. I have both get and post methods in my workspace, but I didn't import them myself and so I don't know where they came from. I want to look at the documentation for these methods. In order to find the documentation, I need to know what module they're defined in. I'm trying to get the fully qualified names in order to figure out which module they're from.
回答1:
I figured it out. Andrew Marshall left a comment that pointed me in the right direction.
First I installed pry, by including it in my gemfile and running bundle install.
Then I inserted binding.pry into my test code. This dropped me into a pry session when I ran my tests.
In the pry session I typed the ls command, which listed all the variables in my context. I found get and post under Rack::Test::Methods#methods.
Rack::Test::Methods#methods: _current_session_names authorize basic_authorize build_rack_mock_session build_rack_test_session clear_cookies current_session delete digest_authorize follow_redirect! get head header last_request last_response options post put rack_mock_session rack_test_session request set_cookie with_session
So, one way to find out the FQN of a method in your workspace is to run Pry's ls command, and then examine the output.
来源:https://stackoverflow.com/questions/12255303/how-to-get-the-fully-qualified-name-of-method-in-ruby