English 中文(简体)
通过字符串引用对象属性
原标题:Referencing an object attribute by a string

我正在尝试DRY一种方法,在这种方法中,我需要对三个不同的属性执行相同的任务。这样地:

if [email protected]?
  picture_copy = Picture.new
  picture_copy.save!
  item_copy.picture = picture_copy
end

if [email protected]_for_x.blank?
  picture_for_x_copy = PictureForX.new
  picture_for_x_copy.save!
  item_copy.picture_for_x = picture_for_x_copy
end

if [email protected]_for_y.blank?
  picture_for_y_copy = PictureForY.new
  picture_for_y_copy.save!
  item_copy.picture_for_y = picture_for_y_copy
end

所以基本上我运行的是相同的代码,但实例化不同的对象,然后将它们分配给不同的属性。感觉应该有一种方法可以使用反射来干燥这个视图。有没有一种方法可以将这些属性和对象称为传递给辅助方法的字符串?

由于各种原因,我不能只使用.clone或.dup:主要是因为涉及二进制文件指针,我还需要深度副本。

最佳回答
{
  picture:       Picture,
  picture_for_x: PictureForX,
  picture_for_y: PictureForY
}.each do |name,klass|
  if [email protected](name).blank?
    copy = klass.new
    copy.save!
    item_copy.send("#{name}=",copy)
  end
end

记住,在Ruby中,没有外部可用的属性或特性,只有方法(你可以选择不带括号地调用方法,这样看起来就像你在访问一个属性,有时可能只是返回一个实例变量的值)。

对象#send是一个神奇的方法,它允许您根据存储在变量中的名称调用方法。

问题回答
def picture_for_x_blank?(s = "")
  s = "_for_#{s}" unless s.empty?
  m = "picture#{s}"

  unless @item.send(m).blank?
    copy = Kernel::const_get(m.camelize).new
    copy.save!
    item_copy.send("#{m}=", copy)
  end
end

picture_for_x_blank?
picture_for_x_blank?("x")
picture_for_x_blank?("y")




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...