我有变量
<% 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
该值没有被分配。有没有办法动态地给变量赋值?
我认为没有办法做到这一点。有实例或类变量,但很少有必要使用局部变量。
在你的情况下,你确实应该将数据放在哈希中。而且,这样的逻辑确实不属于erb。你想要类似的东西:
working_hour_sets = %w[mon tue wed thu fri sat sun].inject({}) do |hash, day|
hash[day]=0;
hash
end
# puts working_hour_sets #=> {"wed"=>0, "sun"=>0, "thu"=>0, "mon"=>0, "tue"=>0, "sat"=>0, "fri"=>0}
working_hour_sets.each do |day, value|
working_hour_sets[day] = 1 if condition?
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)