如何设置“动态”变量值?

时间:2011-08-16 13:51:45

标签: ruby-on-rails ruby ruby-on-rails-3 variables metaprogramming

我正在使用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方法...是否可以做到我需要使用send方法?!)。

5 个答案:

答案 0 :(得分:60)

attr_accessor :variable1, :variable2, :variable3

def set_variables(*attributes)
  attributes.each {|attribute| self.send("#{attribute}=", true)}
end

答案 1 :(得分:9)

以下是sendinstance_variable_set的基准比较:

require 'benchmark'

class Test
  VAR_NAME = '@foo'
  ATTR_NAME = :foo

  attr_accessor ATTR_NAME

  def set_by_send i
    send("#{ATTR_NAME}=", i)
  end

  def set_by_instance_variable_set i
    instance_variable_set(VAR_NAME, i)
  end
end

test = Test.new

Benchmark.bm do |x|
  x.report('send                 ') do
    1_000_000.times do |i|
      test.set_by_send i
    end
  end
  x.report('instance_variable_set') do
    1_000_000.times do |i|
      test.set_by_instance_variable_set i
    end
  end
end

时间安排如下:

      user     system      total        real
send                   1.000000   0.020000   1.020000 (  1.025247)
instance_variable_set  0.370000   0.000000   0.370000 (  0.377150)

(使用1.9.2测量)

应该注意的是,仅在某些情况下(例如,使用attr_accessor定义的访问者),sendinstance_variable_set功能相同。如果涉及的访问器中存在某些逻辑,则会有所不同,您将不得不决定两者中需要哪种变体。 instance_variable_set只是设置了ivar,而send实际上执行了访问器方法,无论它做什么。

另一个评论 - 这两种方法在另一个方面表现不同:如果你instance_variable_set一个尚不存在的ivar,它将被创建。如果使用send调用不存在的访问者,则会引发异常。

答案 2 :(得分:4)

您所使用的方法是instance_variable_set,所以在您的情况下:

def set_variables(*attributes)
  attributes.each {|attribute| self.instance_variable_set(attribute, true)}
end

答案 3 :(得分:3)

def set_attributes(*attributes)
  attributes.each do |attr|
    self.send "#{attr}=", true
  end
end

请记住,setter方法名称以Ruby中的=结尾。

答案 4 :(得分:1)

我知道Rails 3的问题,但是在搜索Rails 4关于“如何动态访问变量值”的答案时出现了问题。我在我的模型上对此进行了测试,并且它可以替代所提出的解决方案:

def set_variables(*attributes)
  attributes.each {|attribute| self["#{attribute}"] = true}
end