【发布时间】:2014-01-30 20:20:02
【问题描述】:
我有办法
def populate destination, source
end
其中 destination 始终是以下值之一:“ax”、“bx”、“cx”、“dx”。在包含该方法的类中,我有@ax、@bx、@cx、@dx。如果 destination 动态变化并且我知道它在方法体中运行时,我如何从方法体中分配正确的属性(属性)。
我正在尝试这样的 send 方法:
self.send(destination, source)
但它给了我一个错误。
我已经定义了这样的属性:
attr_accessor :ax, :bx, :cx, :dx
编辑:
方法本身:
def populate destination, source
receiver = destination
if source == :ax then self.send(:populate, receiver , @ax_real) end
if source == :bx then self.send(:populate, receiver , @bx_real) end
if source == :cx then self.send(:populate, receiver , @cx_real) end
if source == :dx then self.send(:populate, receiver , @dx_real) end
if source.class != Symbol then self.send(:populate, receiver , source) end
end
这让我陷入了无休止的递归。
【问题讨论】:
-
你想在这里做什么
if source == :ax then self.send(:populate, receiver , @ax_real) end? -
你是在尝试做
@ax= :ax,@bx= :bx等吗? -
如果源是 :ax 那么我想设置来自目标的属性值(ax, bx, cx, dx) 并将其分配给另一个属性@ax_real 的值。例如。如果接收者是 :cx 我想要这个 '@cx_real' = '@ax_real'。
-
@ArupRakshit 是的,就是这样。但这取决于。可能是“@bx”、“@cx”等。
标签: ruby methods properties attributes variable-assignment