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

ruby-changes:62274

From: Jim <ko1@a...>
Date: Mon, 20 Jul 2020 03:35:17 +0900 (JST)
Subject: [ruby-changes:62274] b219cd5ac3 (master): [ruby/csv] Make CSV::Row#dup return a usable Row (#108)

https://git.ruby-lang.org/ruby.git/commit/?id=b219cd5ac3

From b219cd5ac36ffb733e3eccd98d53ecf660dee5bf Mon Sep 17 00:00:00 2001
From: Jim Kane <fastjames@g...>
Date: Tue, 22 Oct 2019 05:01:24 -0500
Subject: [ruby/csv] Make CSV::Row#dup return a usable Row (#108)

* Make CSV::Row#dup return a usable Row

Previously, calling `dup` on a `CSV::Row` object yielded an object whose
copy was too shallow. Changing the clone's fields would also change the
fields on the source. This change makes the clone more distinct from the
source, so that changes can be made to its fields without affecting the
source.

* Simplify

https://github.com/ruby/csv/commit/64a1ea06fc

diff --git a/lib/csv/row.rb b/lib/csv/row.rb
index 4aa0f30..a2d03e8 100644
--- a/lib/csv/row.rb
+++ b/lib/csv/row.rb
@@ -50,7 +50,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv/row.rb#L50
 
     def initialize_copy(other)
       super
-      @row = @row.dup
+      @row = @row.collect(&:dup)
     end
 
     # Returns +true+ if this is a header row.
diff --git a/test/csv/test_row.rb b/test/csv/test_row.rb
index f709dd3..b717945 100644
--- a/test/csv/test_row.rb
+++ b/test/csv/test_row.rb
@@ -425,6 +425,9 @@ class TestCSVRow < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/csv/test_row.rb#L425
   def test_dup
     row = CSV::Row.new(["A"], ["foo"])
     dupped_row = row.dup
+    dupped_row["A"] = "bar"
+    assert_equal(["foo", "bar"],
+                 [row["A"], dupped_row["A"]])
     dupped_row.delete("A")
     assert_equal(["foo", nil],
                  [row["A"], dupped_row["A"]])
-- 
cgit v0.10.2


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

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