0

我敢肯定,这是一个奇怪的问题。我希望能够做这样的事情:

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++ 中,可以使用类型化操作数覆盖赋值运算符。有没有我没有想到的性感解决方案?

4

1 回答 1

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
于 2013-06-07T21:14:11.397 回答