1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
zzak 2013-11-21 05:19:32 +00:00
parent 5d1d1ed258
commit 6869a65a8c
2 changed files with 39 additions and 7 deletions

View file

@ -1,3 +1,10 @@
Thu Nov 21 14:18:24 2013 Zachary Scott <e@zzak.io>
* 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 <ko1@atdot.net> Thu Nov 21 14:06:02 2013 Koichi Sasada <ko1@atdot.net>
* gc.c (gc_marks_check): do not dump all refs. * gc.c (gc_marks_check): do not dump all refs.

View file

@ -356,17 +356,42 @@ rb_obj_clone(VALUE obj)
* obj.dup -> an_object * obj.dup -> an_object
* *
* Produces a shallow copy of <i>obj</i>---the instance variables of * Produces a shallow copy of <i>obj</i>---the instance variables of
* <i>obj</i> are copied, but not the objects they reference. * <i>obj</i> are copied, but not the objects they reference. <code>dup</code>
* <code>dup</code> copies the tainted state of <i>obj</i>. See also * copies the tainted state of <i>obj</i>.
* the discussion under <code>Object#clone</code>. In general,
* <code>clone</code> and <code>dup</code> may have different semantics
* in descendant classes. While <code>clone</code> is used to duplicate
* an object, including its internal state, <code>dup</code> typically
* uses the class of the descendant object to create the new instance.
* *
* This method may have class-specific behavior. If so, that * This method may have class-specific behavior. If so, that
* behavior will be documented under the #+initialize_copy+ method of * behavior will be documented under the #+initialize_copy+ method of
* the class. * the class.
*
* === on dup vs clone
*
* In general, <code>clone</code> and <code>dup</code> may have different
* semantics in descendant classes. While <code>clone</code> is used to
* duplicate an object, including its internal state, <code>dup</code>
* 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 #=> #<Klass:0x401b3a38>
* s1.extend(Foo) #=> #<Klass:0x401b3a38>
* s1.foo #=> "foo"
*
* s2 = s1.clone #=> #<Klass:0x401b3a38>
* s2.foo #=> "foo"
*
* s3 = s1.dup #=> #<Klass:0x401b3a38>
* s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401b3a38>
*
*/ */
VALUE VALUE