Ruby defines #clone in Object.
To my suprise, some classes raise Exceptions when calling it.
I found NilClass, TrueClass, FalseClass, Fixnum having this behaviour.
1) Does a complete list of classes (at least core-classes) exist, which do not allow #clone ?
Or is there a way to detect if a specific class supports #clone ?
2) What is wrong with 42.clone ?
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.clonereturn the same42, but then the invariantobj.clone.object_id != obj.object_idwould no longer hold,42.clonewouldn’t actually be cloning.