我有变量字符串转换为变量名红宝石
<% mon_has_two_sets_of_working_hours = 0 %>
<% tue_has_two_sets_of_working_hours = 0 %>
<% wed_has_two_sets_of_working_hours = 0 %>
我要动态地改变这些变量的值。
<% days_array = ['mon', 'tue', 'wed'] %>
<% days_array.each do |day| %>
<% if condition? %>
# here i want to set %>
<% "#{day}__has_two_sets_of_working_hours" = 1 %>
end
end
该值未被赋值。有没有办法动态地赋值给变量?
做,做,使用数组(或哈希)。 – 2011-01-08 12:10:17
[2530112](http://stackoverflow.com/questions/2530112)的答案应该有所帮助,它建议`instance_variable_set`。 – 2011-01-08 12:18:18