Which Ruby classes support .clone?

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

    Fixnum is a special class given special treatment by the language. From the time your program launches, there is precisely one Fixnum for every number that the class can represent, and they're given a special representation that doesn't take any extra space — this way, basic math operations aren't allocating and deallocating memory like crazy. Because of this, there cannot be more than one 42.

    For the others, they all have one thing in common: They're singletons. There's only one instance of a singleton class by definition, so trying to clone it is an error.

提交回复
热议问题