def method
  a = 3
  b = 4

  some_method_that_gives # [a, b] 
end
有帮助吗?

解决方案

local_variables

它输出符号数组,显示变量。在您的情况下: [:a, :b]

其他提示

local_variables 列出本地变量,但在定义之前列出了它们。看到这个:

p local_variables
a = 1
p local_variables

这输出

[:a]
[:a]

这可能不是您所期望的。与 defined?

p defined? a
a = 1
p defined? a

输出更加预期的

nil
"local-variable"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top