我正在尝试使用eval
和改变局部变量数组在 Ruby 中动态创建局部变量。我在 IRB 做这个。
eval "t = 2"
local_variables # => [:_]
eval "t"
# => NameError: undefined local variable or method `t' for main:Object
local_variables << "t".to_sym # => [:_, :t]
t
# => NameError: undefined local variable or method `t' for main:Object