在Rails中键入等于覆盖

时间:2013-06-07 20:41:33

标签: ruby-on-rails model override variable-assignment

我确信这是一个奇怪的问题。我希望能够做到这样的事情:

crazy_model = CrazyModel.new(some: "initialization", hashy: "options") 
tame_model  = TameModel.new (other: "init", hashy: "things")

tame_model = crazy_model

然后以编程方式决定如何将crazy_model属性分配给tame_model。在C ++中,可以使用类型化的操作数覆盖赋值运算符。有没有我想到的性感解决方案?

1 个答案:

答案 0 :(得分:0)

我能想到的最接近的是

crazy_model = CrazyModel.new(some: "initialization", hashy: "options") 
tame_model  = TameModel.new (other: "init", hashy: "things")

converted_model = crazy_model.to_tame_model


class CrazyModel
 def to_tame_model
   TameModel.new({
     #assign the attributes here
   })
 end
end