gpt4 book ai didi

ruby - 你如何在 ruby​​ 中列出当前范围内的当前可用对象?

转载 作者:数据小太阳 更新时间:2023-10-29 06:24:22 26 4
gpt4 key购买 nike

我是 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__']
>>>

最佳答案

我不完全确定您所说的“当前对象”是什么意思。正如已经提到的,您可以遍历 ObjectSpace。但这里有一些其他方法。

local_variables
instance_variables
global_variables

class_variables
constants

有一个陷阱。必须在正确的范围内调用它们。所以就在 IRB 中,或者在一个对象实例中,或者在类范围内(基本上无处不在),你可以调用前 3 个。

local_variables #=> ["_"]
foo = "bar"
local_variables #=> ["_", "foo"]
# Note: the _ variable in IRB contains the last value evaluated
_ #=> "bar"

instance_variables #=> []
@inst_var = 42
instance_variables #=> ["@inst_var"]

global_variables #=> ["$-d", "$\"", "$$", "$<", "$_", ...]
$" #=> ["e2mmap.rb", "irb/init.rb", "irb/workspace.rb", ...]

但是,嗯,如果您希望您的程序实际评估它们而不需要您多次键入它们怎么办?诀窍是评估。

eval "@inst_var" #=> 42
global_variables.each do |v|
puts eval(v)
end

开头提到的 5 个中的最后 2 个必须在模块级别进行评估(类是模块的后代,因此有效)。

Object.class_variables #=> []
Object.constants #=> ["IO", "Duration", "UNIXserver", "Binding", ...]

class MyClass
A_CONST = 'pshh'
class InnerClass
end
def initialize
@@meh = "class_var"
end
end

MyClass.constants #=> ["A_CONST", "InnerClass"]
MyClass.class_variables #=> []
mc = MyClass.new
MyClass.class_variables #=> ["@@meh"]
MyClass.class_eval "@@meh" #=> "class_var"

这里有一些在不同方向探索的技巧

"".class            #=> String
"".class.ancestors #=> [String, Enumerable, Comparable, ...]
String.ancestors #=> [String, Enumerable, Comparable, ...]

def trace
return caller
end
trace #=> ["(irb):67:in `irb_binding'", "/System/Library/Frameworks/Ruby...", ...]

关于ruby - 你如何在 ruby​​ 中列出当前范围内的当前可用对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/228648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com