为什么local_variables
返回尚未赋值的局部变量(调用后赋值local_variables
)?
a = 2
@aa = 1
# a = b # this will raise an error.
puts "local: #{ local_variables }"
puts "instance: #{ instance_variables }"
b = 2
@bb = 2
puts "local: #{ local_variables }"
puts "instance: #{ instance_variables }"
结果:
local: [:a, :b]
instance: [:@aa]
local: [:a, :b]
instance: [:@aa, :@bb]
我期望的是像 那样的行为instance_variables
,它只返回在那一刻已经分配的变量。