如何在 ruby 中列出当前范围内当前可用的对象?
我是 ruby 新手,正在使用 IRB。
我发现我可以使用“.methods”方法列出对象的方法,并且 self.methods 可以给我我想要的东西(类似于 Python 的 dir(builtins)?),但是如何我可以找到通过 include 和 require 加载的库/模块的方法吗?
irb(main):036:0* self.methods
=> ["irb_pop_binding", "inspect", "taguri", "irb_chws", "clone", "irb_pushws", "public_methods", "taguri=", "irb_pwws",
"public", "display", "irb_require", "irb_exit", "instance_variable_defined?", "irb_cb", "equal?", "freeze", "irb_context
", "irb_pop_workspace", "irb_cwb", "irb_jobs", "irb_bindings", "methods", "irb_current_working_workspace", "respond_to?"
, "irb_popb", "irb_cws", "fg", "pushws", "conf", "dup", "cwws", "instance_variables", "source", "cb", "kill", "help", "_
_id__", "method", "eql?", "irb_pwb", "id", "bindings", "send", "singleton_methods", "popb", "irb_kill", "chws", "taint",
"irb_push_binding", "instance_variable_get", "frozen?", "irb_source", "pwws", "private", "instance_of?", "__send__", "i
rb_workspaces", "to_a", "irb_quit", "to_yaml_style", "irb_popws", "irb_change_workspace", "jobs", "type", "install_alias
_method", "irb_push_workspace", "require_gem", "object_id", "instance_eval", "protected_methods", "irb_print_working_wor
kspace", "irb_load", "require", "==", "cws", "===", "irb_pushb", "instance_variable_set", "irb_current_working_binding",
"extend", "kind_of?", "context", "gem", "to_yaml_properties", "quit", "popws", "irb", "to_s", "to_yaml", "irb_fg", "cla
ss", "hash", "private_methods", "=~", "tainted?", "include", "irb_cwws", "irb_change_binding", "irb_help", "untaint", "n
il?", "pushb", "exit", "irb_print_working_binding", "is_a?", "workspaces"]
irb(main):037:0>
我习惯了 python,我使用 dir() 函数来完成同样的事情:
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>>
I'm new to ruby and I'm playing around with the IRB.
I found that I can list methods of an object using the ".methods" method, and that self.methods sort of give me what I want (similar to Python's dir(builtins)?), but how can I find the methods of a library/module I've loaded via include and require?
irb(main):036:0* self.methods
=> ["irb_pop_binding", "inspect", "taguri", "irb_chws", "clone", "irb_pushws", "public_methods", "taguri=", "irb_pwws",
"public", "display", "irb_require", "irb_exit", "instance_variable_defined?", "irb_cb", "equal?", "freeze", "irb_context
", "irb_pop_workspace", "irb_cwb", "irb_jobs", "irb_bindings", "methods", "irb_current_working_workspace", "respond_to?"
, "irb_popb", "irb_cws", "fg", "pushws", "conf", "dup", "cwws", "instance_variables", "source", "cb", "kill", "help", "_
_id__", "method", "eql?", "irb_pwb", "id", "bindings", "send", "singleton_methods", "popb", "irb_kill", "chws", "taint",
"irb_push_binding", "instance_variable_get", "frozen?", "irb_source", "pwws", "private", "instance_of?", "__send__", "i
rb_workspaces", "to_a", "irb_quit", "to_yaml_style", "irb_popws", "irb_change_workspace", "jobs", "type", "install_alias
_method", "irb_push_workspace", "require_gem", "object_id", "instance_eval", "protected_methods", "irb_print_working_wor
kspace", "irb_load", "require", "==", "cws", "===", "irb_pushb", "instance_variable_set", "irb_current_working_binding",
"extend", "kind_of?", "context", "gem", "to_yaml_properties", "quit", "popws", "irb", "to_s", "to_yaml", "irb_fg", "cla
ss", "hash", "private_methods", "=~", "tainted?", "include", "irb_cwws", "irb_change_binding", "irb_help", "untaint", "n
il?", "pushb", "exit", "irb_print_working_binding", "is_a?", "workspaces"]
irb(main):037:0>
I'm used to python, where I use the dir() function to accomplish the same thing:
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不完全确定“当前对象”是什么意思。 正如已经提到的,您可以迭代 ObjectSpace。 但这里还有一些其他方法。
有一个问题。 必须在正确的范围内调用它们。 因此,在 IRB 中,或者在对象实例中,或者在类作用域中(基本上在任何地方),您都可以调用前 3 个。
但是,嗯,如果您希望程序实际评估它们而不需要多次键入它们,该怎么办? 诀窍是评估。
开头提到的 5 个中的最后 2 个必须在模块级别进行评估(类是模块的后代,因此可以工作)。
这里还有一些可以在不同方向探索的技巧
I'm not entirely sure of what you mean by the 'current objects'. You can iterate over ObjectSpace, as has been mentioned already. But here are a few other methods.
There's one gotcha. They must be called at the right scopes. So right in IRB, or in an object instance or at class scope (so everywhere, basically) you can call the first 3.
But umm, what if you want your program to actually evaluate them without needing you to type them manyally? The trick is eval.
The last 2 of the 5 mentioned at the beginning must be evaluated at the module level (a class is a descendant of a module, so that works).
Here's are a few more tricks to explore in different directions
ObjectSpace.each_object 可能就是您正在寻找的为了。
要获取包含的模块列表,您可以使用 Module.included_modules 。
您还可以使用 object.respond_to?。
ObjectSpace.each_object could be what you are looking for.
To get a list of included modules you could use Module.included_modules.
You can also check if an object responds to a method on a case-by-case basis using object.respond_to?.
dir()
方法是 没有明确定义......但我们可以用 Ruby 创建一个近似值。 让我们创建一个方法,该方法将返回由包含的模块添加到我们的范围的所有方法的排序列表。 我们可以使用
included_modules
方法获取已包含的模块列表。像
dir()
一样,我们希望忽略“默认”方法(例如print
),并且我们还希望专注于“有趣”的名称集。 因此,我们将忽略Kernel
中的方法,并且只会返回直接在模块中定义的方法,忽略继承的方法。 我们可以通过将false
传递到methods()
方法来完成后者。 把它们放在一起我们得到......你可以向它传递一个类、一个对象或什么都没有(它默认为当前范围)。 让我们尝试一下...
dir()
还包括本地定义的变量,这是一个简单的方法。 只需调用......不幸的是,我们不能只将
local_variables
调用添加到included_methods
中,因为它会给我们本地的变量Included_methods
方法,这不会很有用。 因此,如果您希望局部变量包含在included_methods中,只需调用...The
dir()
method is not clearly defined......but we can create a close approximation in Ruby. Let's make a method that will return a sorted list of all methods added to our scope by included modules. We can get a list of the modules that have been included by using the
included_modules
method.Like
dir()
, we want to ignore the "default" methods (likeprint
), and we also want to focus on the "interesting" set of names. So, we will ignore methods inKernel
, and we will only return methods that were defined directly in the modules, ignoring inherited methods. We can accomplish the later by passingfalse
into themethods()
method. Putting it all together we get...You can pass it a class, an object, or nothing (it defaults to the current scope). Let's try it out...
dir()
also includes locally defined variables, and that's an easy one. Just call......unfortunately, we can't just add the
local_variables
call toincluded_methods
because it would give us the variables that are local to theincluded_methods
method, and that wouldn't be very useful. So, if you want local variables included with the included_methods, just call...我为此编写了一个 gem:
我正在努力改进传递选项和设置默认值,但现在我建议您将以下内容添加到 .irbrc 文件中:
这会启用颜色并隐藏每个对象具有的方法,因为您通常对这些不感兴趣。
I wrote a gem for that:
I am working on an improvement of passing options and settings defaults, but for now I would suggest you add the following to your .irbrc file:
This enables colours and hides the methods that every object has, since you're usually not interested in those.
怎么样:
列出了我可以选择的课程。 我不是红宝石专家,我被丢在红宝石控制台上,不知道手头有什么课程。 那艘班轮是一个开始。
What about:
That lists available classes for me. I am not ruby expert and I was being dropped at a ruby console with no idea of what classes were at hand. That one liner was a start.
要访问 ruby 中的所有对象实例,请使用 ObjectSpace
http: //www.ruby-doc.org/core-1.8.7/classes/ObjectSpace.html#M000928
但是,这被认为很慢(即使对于 ruby),并且在某些解释器中可能无法启用(例如 jRuby)可以禁用 ObjectSpace,因为它依靠 jvm 进行 gc 更快,而无需在 jRuby 中跟踪这些东西)。
To access all object instances in ruby you use ObjectSpace
http://www.ruby-doc.org/core-1.8.7/classes/ObjectSpace.html#M000928
However, this is considered slow (even for ruby), and may not be enabled in some interpreters (e.g. jRuby can disable ObjectSpace as it is much faster relying in the jvm for gc without needing to track this stuff in jRuby).
您甚至可以在加载之前将 .methods 消息传递到库/模块,以查看所有可用的方法。 执行 self.methods 只会返回 Object 对象包含的所有方法。 您可以通过执行
self.class
来看到这一点。 假设您想查看 File 模块中的所有方法。 您只需执行File.methods
即可获得 File 模块中存在的所有方法的列表。 这也许不是您想要的,但它应该有所帮助。You can pass the .methods messages to the library/module even before loading it, to see all the available methods. Doing
self.methods
just returns all the methods that the Object object contains. You can see this by doingself.class
. So let's say you want to see all the methods in the File module. You simply doFile.methods
and you'll get a list of all the methods that exist in the File module. This, perhaps, isn't what you want, but it should be somewhat helpful.