object.c defines clone method for objects that cannot be cloned.
As the subject says, in object.c, the clone method is defined and then special cased for certain object types. The end result is that all respond_to?(:clone) returns true for all objects, but then thows an fatal error in some cases. Here is an appropriate example:
a = true
TypeError: can't clone TrueClass
from (irb):3:in `clone'
Ultimately, the objects that do no respond to 'clone' should have it removed so that the respond_to? method returns false.
Updated by akr (Akira Tanaka) almost 7 years ago
2012/11/5 mame (Yusuke Endoh) email@example.com:
Issue #7216 has been updated by mame (Yusuke Endoh).
Akr-san, what do you think?
It may be good idea.
The root problem is that Liskov substitution principle is violated
between Object and TrueClass.
(Object is clonable but its subclass, TrueClass, is not.)
So, such unprincipled classes may have responsibility to
undefine/unimplement methods which don't work.
I feel it's better to ask matz.
Updated by jeremyevans0 (Jeremy Evans) about 2 months ago
- Status changed from Assigned to Closed
Ruby started allowing
clone on immediate values (true, false, nil, symbol, integer, float) in Ruby 2.4:
$ ruby23 -e 'p true.clone' -e:1:in `clone': can't clone TrueClass (TypeError) from -e:1:in `<main>' $ ruby24 -e 'p true.clone' true