Which Ruby classes support .clone?

前端 未结 6 907
自闭症患者
自闭症患者 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:13

    You can't clone immutable classes. I.e. you can have only one instance of object 42 (as a Fixnum), but can have many instances of "42" (because string is mutable). You can't clone symbols as well since they are something like immutable strings.

    You can check that in IRB with object_id method. (symbols and fixnums will give you same object_id after repetitive calls)

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题