Which Ruby classes support .clone?

前端 未结 6 902
自闭症患者
自闭症患者 2021-01-05 02:48

Ruby defines #clone in Object. To my suprise, some classes raise Exceptions when calling it. I found NilClass, TrueCla

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 03:05

    I don't think there is a formal list, at least unless you count reading the source. The reason 2) doesn't work is because of an optimization applied to Fixnums. They are stored/passed internally as their actual values (so are true, false and nil), and not as pointers. The naive solution is to just have 42.clone return the same 42, but then the invariant obj.clone.object_id != obj.object_id would no longer hold, 42.clone wouldn't actually be cloning.

提交回复
热议问题