指派属性我有一个方法如何动态地从法红宝石
def populate destination, source
end
其中目的地始终是以下值之一:“斧”,“BX”,“CX”,“DX” 。在包含该方法的类中,我有@ax,@ bx,@cx,@dx。如果目标动态变化,并且我知道它在方法主体中的运行时间,如何从方法主体分配正确的属性(属性)。
我是用试图发送方法是这样的:
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 @,@ @ @ = = bx等等,,? –
如果source是:ax那么我想设置来自目的地(ax,bx,cx,dx)的属性值并将其赋值为另一个属性@ax_real的值。 例如如果接收者是:cx我想要这个'@cx_real'='@ax_real'。 – user2128702