我在使用Ruby on Rails 3.0.9,我正在尝试设置“动态”一些变量值。那是…
…在我的模型文件我有:
attr_accessor :variable1, :variable2, :variable3
# The 'attributes' argument contains one or more symbols which name is equal to
# one or more of the 'attr_accessor' symbols.
def set_variables(*attributes)
# Here I should set to 'true' all ":variable<N>" attributes passed as symbol
# in the 'attributes' array, but variable names should be interpolated in a
# string.
#
# For example, I should set something like "prefix_#{':variable1'.to_s}_suffix".
end
如何将这些变量值设置为true?
我试图使用self.send(…)方法,但我没有成功(但是,可能我不知道如何使用发送方法…是可能做到这一点我需要使用send方法?!)。
attr_accessor :variable1, :variable2, :variable3
def set_variables(*attributes)
attributes.each {|attribute| self.send("#{attribute}=", true)}
end
相关文章
转载注明原文:ruby-on-rails – 如何设置“动态”变量值? - 代码日志