How to list local-variables in Ruby?

末鹿安然 提交于 2019-12-04 22:17:56
Nakilon

local_variables

It outputs array of symbols, presenting variables. In your case: [:a, :b]

local_variables lists local variables but it lists them before they are defined. See this:

p local_variables
a = 1
p local_variables

this outputs

[:a]
[:a]

which may not be what you expect. Contrast with defined?

p defined? a
a = 1
p defined? a

which outputs the more anticipated

nil
"local-variable"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!