Project

General

Profile

Actions

Bug #7216

closed

object.c defines clone method for objects that cannot be cloned.

Bug #7216: object.c defines clone method for objects that cannot be cloned.

Added by therevmj (Michael Johnson) about 13 years ago. Updated about 6 years ago.

Status:
Closed
Target version:
-
ruby -v:
all versions up to current trunk
[ruby-core:48292]

Description

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
=> true
a.respond_to?(:clone)
=> true
a.clone
TypeError: can't clone TrueClass
from (irb):3:in `clone'
from (irb):3

Ultimately, the objects that do no respond to 'clone' should have it removed so that the respond_to? method returns false.

Updated by mame (Yusuke Endoh) almost 13 years ago Actions #1 [ruby-core:48917]

  • Status changed from Open to Assigned
  • Assignee set to akr (Akira Tanaka)
  • Target version set to 2.0.0

Akr-san, what do you think?

--
Yusuke Endoh

Updated by akr (Akira Tanaka) almost 13 years ago Actions #2 [ruby-core:48968]

2012/11/5 mame (Yusuke Endoh) :

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.

Tanaka Akira

Updated by trans (Thomas Sawyer) over 12 years ago Actions #3 [ruby-core:52394]

This is true for class method #allocate too.

Updated by mame (Yusuke Endoh) over 12 years ago Actions #4 [ruby-core:52471]

  • Assignee changed from akr (Akira Tanaka) to matz (Yukihiro Matsumoto)
  • Target version changed from 2.0.0 to 2.6

Updated by naruse (Yui NARUSE) almost 8 years ago Actions #5

  • Target version deleted (2.6)

Updated by jeremyevans0 (Jeremy Evans) about 6 years ago Actions #6 [ruby-core:94165]

  • 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
Actions

Also available in: PDF Atom