【发布时间】:2011-01-21 01:59:46
【问题描述】:
我有一个项目ActiveRecords,我正在尝试使用一个块为每个项目设置一个默认值(“测试项目”)。
在这个表达式中:
list = {"type1", "type2", "type3", "type4", "..."}
list.each { |name| @item.attributes["#{name}"] = "Test item"] }
值未设置。
我必须使用@item.attributes["#{name}"] 进行插值,因为我不能对每个项目都这样做:
@item.tipe1 = "Test item"
那么,第一个语句会发生什么?为什么?如果我想做的事不能那样做,我怎么能做同样的事?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 block proc-object