如何在ruby中列出当前作用域中的当前可用对象? [英] How do you list the currently available objects in the current scope in ruby?

查看:83
本文介绍了如何在ruby中列出当前作用域中的当前可用对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是红宝石的新手,并且正在玩IRB.

I'm new to ruby and I'm playing around with the IRB.

我发现我可以使用".methods"方法列出对象的方法,并且self.methods可以给我我想要的东西(类似于Python的dir( builtins )?) ,但是如何找到通过include和require加载的库/模块的方法?

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>

我习惯了python,在这里我使用dir()函数来完成相同的事情:

I'm used to python, where I use the dir() function to accomplish the same thing:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>>

推荐答案

ObjectSpace.each_object 可能就是您想要的.

要获取包含的模块列表,可以使用 Module.included_modules .

To get a list of included modules you could use Module.included_modules.

您还可以使用 object.respond_to?.

这篇关于如何在ruby中列出当前作用域中的当前可用对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆