将一个对象的多个值分配给另一个对象
我正在尝试编写一个 ruby 函数,它迭代作为方法名称的字符串数组,并使用 Object#send
方法将值保存在新实例对象中(甚至不确定这是否合法)是对已经存在的一个的转变。这是我能解释的最好的了。想法是这样的:
@example = RelatedClass.new
def example_method
instance_dependant_float = related_class.myvalue / other_related_class.myvalue
ARRAY_OF_METHODS.each do |t|
@example.send(t+'=', self.related_class.t * instance_dependant_float)
end
end
当我尝试运行这样的东西时,我在两个不同的场合(在我的发送中和在乘法器中)调用索引“t”,第二次出现 NoMethodError 。
I am trying to write a ruby function that iterates through an array of strings that are method names and uses the Object#send
method to save values in a new instance object (not even sure if thats legit) that is a transformation of an already existing one. That's the best I can explain it. Here's the idea:
@example = RelatedClass.new
def example_method
instance_dependant_float = related_class.myvalue / other_related_class.myvalue
ARRAY_OF_METHODS.each do |t|
@example.send(t+'=', self.related_class.t * instance_dependant_float)
end
end
When I try to run something like this where I call the index "t" on two separate ocassions (in my send and in the multiplier) it NoMethodError's on the second ocassion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还需要
send
来获取值:我总是使用并提倡在
String#+
上进行字符串插值,并且您不需要self .
获取lated_class
。You need to
send
to get the value also:I always use and advocate string interpolation over
String#+
, and you don't need theself.
to get therelated_class
.