Ruby 怎样用 A 变量的值作为 B 变量的名啊?

chenryn · May 11, 2012 · Last by kingwmj replied at May 14, 2012 · 2902 hits

比方 i=1 data1=123 p data#{i}的结果是 data1,我想要 123 怎么做?

eval set_instance_var

p eval "data#{i}"

实例变量的话使用 instance_variable_get('') 类变量的话使用 class_variable_get('') 另外,是局部变量的话,好像 api 中只有 local_variables 可用

这种情况用数组或是 hash 吧。

You need to Sign in before reply, if you don't have an account, please Sign up first.