首先,这是@market_0_home 在下面迭代之外的分配,只是为了向您展示我尝试过
>> @market_0_home = 3
=> 3
好的,那里没有错误,现在让我们在一个有点复杂的迭代中尝试一下
>> markets
=> [{"home"=>"CO", "name"=>"David Douglas"}, {"home"=>"SC", "name"=>"David Robertson"}]
>> markets.each_with_index do |market, i|
?> market.each do |name, v|
?> instance_variable_set "market_#{i}_#{name}", v
>> end
>> end
NameError: `market_0_home' is not allowed as an instance variable name
from (irb):23:in `instance_variable_set'
from (irb):23
from (irb):22:in `each'
from (irb):22
from (irb):25:in `each_with_index'
from (irb):21:in `each'
from (irb):21:in `each_with_index'
from (irb):21
>>