From 6869a65a8c6801984ae5bfc1b793843933925ef6 Mon Sep 17 00:00:00 2001 From: zzak Date: Thu, 21 Nov 2013 05:19:32 +0000 Subject: * 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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43747 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ object.c | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) 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 -- cgit v1.2.3