如何获得Ruby中方法的完全限定名? [英] How to get the fully qualified name of method in Ruby?
问题描述
假设我在Ruby的工作区中有一个可用的方法。是否有一个命令我可以使用,将恢复的方法的完全限定名?
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?
我在寻找类似其中函数在Matlab中,为任何参数返回一个明确的名称。
I'm looking for something similar to the which function in Matlab, that returns an unambiguous name for any argument it's given.
我问,因为我在Cucumber中写一个测试,需要发送特定的GET和POST请求。我在工作区中有 get
和方法,但我没有自己导入它们,所以我不'知道他们来自哪里。我想看看这些方法的文档。为了找到文档,我需要知道它们定义的模块。我想获得完全限定的名称,以便找出他们来自哪个模块。
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.
推荐答案
我想出来了。
首先我安装了 pry ,通过将其包含在我的gemfile中并运行 bundle install
。
First I installed pry, by including it in my gemfile and running bundle install
.
code> binding.pry 到我的测试代码。
Then I inserted binding.pry
into my test code. This dropped me into a pry session when I ran my tests.
在pry会话中,我输入了 ls
命令,列出了我上下文中的所有变量。我在下找到
。 get
和 post
Rack :: Test :: Methods#methods
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
因此,一种方法来找出您的工作空间中的方法的FQN是运行Pry的 ls
命令,然后检查输出。
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.
这篇关于如何获得Ruby中方法的完全限定名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!