diff --git a/ChangeLog b/ChangeLog index 4bb0548e0e..861d11110c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Thu Nov 21 14:18:24 2013 Zachary Scott + + * object.c: [DOC] Clarify Object#dup vs #clone [Bug #9128] + Moving existing doc for this comparison to separate section of #dup + Adding examples to document behavior of #dup with Module#extend. + Based on a patch by stevegoobermanhill + Thu Nov 21 14:06:02 2013 Koichi Sasada * gc.c (gc_marks_check): do not dump all refs. diff --git a/object.c b/object.c index e7b61d6687..70f6d66e5e 100644 --- a/object.c +++ b/object.c @@ -356,17 +356,42 @@ rb_obj_clone(VALUE obj) * obj.dup -> an_object * * Produces a shallow copy of obj---the instance variables of - * obj are copied, but not the objects they reference. - * dup copies the tainted state of obj. See also - * the discussion under Object#clone. In general, - * clone and dup may have different semantics - * in descendant classes. While clone is used to duplicate - * an object, including its internal state, dup typically - * uses the class of the descendant object to create the new instance. + * obj are copied, but not the objects they reference. dup + * copies the tainted state of obj. * * This method may have class-specific behavior. If so, that * behavior will be documented under the #+initialize_copy+ method of * the class. + * + * === on dup vs clone + * + * In general, clone and dup may have different + * semantics in descendant classes. While clone is used to + * duplicate an object, including its internal state, dup + * typically uses the class of the descendant object to create the new + * instance. + * + * When using #dup any modules that the object has been extended with will not + * be copied. + * + * class Klass + * attr_accessor :str + * end + * + * module Foo + * def foo; 'foo'; end + * end + * + * s1 = Klass.new #=> # + * s1.extend(Foo) #=> # + * s1.foo #=> "foo" + * + * s2 = s1.clone #=> # + * s2.foo #=> "foo" + * + * s3 = s1.dup #=> # + * s3.foo #=> NoMethodError: undefined method `foo' for # + * */ VALUE