[前][次][番号順一覧][スレッド一覧]

ruby-changes:31668

From: zzak <ko1@a...>
Date: Thu, 21 Nov 2013 14:19:40 +0900 (JST)
Subject: [ruby-changes:31668] zzak:r43747 (trunk): * object.c: [DOC] Clarify Object#dup vs #clone [Bug #9128]

zzak	2013-11-21 14:19:32 +0900 (Thu, 21 Nov 2013)

  New Revision: 43747

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43747

  Log:
    * 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

  Modified files:
    trunk/ChangeLog
    trunk/object.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43746)
+++ ChangeLog	(revision 43747)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Nov 21 14:18:24 2013  Zachary Scott  <e@z...>
+
+	* 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@a...>
 
 	* gc.c (gc_marks_check): do not dump all refs.
Index: object.c
===================================================================
--- object.c	(revision 43746)
+++ object.c	(revision 43747)
@@ -356,17 +356,42 @@ rb_obj_clone(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L356
  *     obj.dup -> an_object
  *
  *  Produces a shallow copy of <i>obj</i>---the instance variables of
- *  <i>obj</i> are copied, but not the objects they reference.
- *  <code>dup</code> copies the tainted state of <i>obj</i>. See also
- *  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.
+ *  <i>obj</i> are copied, but not the objects they reference. <code>dup</code>
+ *  copies the tainted state of <i>obj</i>.
  *
  *  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, <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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]