Which Ruby classes support .clone?

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

    I did a git grep "can't clone" of YARV's source code, and got

    lib/singleton.rb:    raise TypeError, "can't clone instance of singleton #{self.class}"
    object.c:        rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
    test/test_singleton.rb:    expected = "can't clone instance of singleton TestSingleton::SingletonTest"
    

    The first and third lines indicate you can't clone a singleton.

    The second line refers to rb_special_const_p(obj). But this is going beyond my ken.

提交回复
热议问题