[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
This commit is contained in:
Jim Kane 2019-10-22 05:01:24 -05:00 коммит произвёл Nobuyoshi Nakada
Родитель 9141aae8c2
Коммит b219cd5ac3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
2 изменённых файлов: 4 добавлений и 1 удалений

Просмотреть файл

@ -50,7 +50,7 @@ class CSV
def initialize_copy(other) def initialize_copy(other)
super super
@row = @row.dup @row = @row.collect(&:dup)
end end
# Returns +true+ if this is a header row. # Returns +true+ if this is a header row.

Просмотреть файл

@ -425,6 +425,9 @@ class TestCSVRow < Test::Unit::TestCase
def test_dup def test_dup
row = CSV::Row.new(["A"], ["foo"]) row = CSV::Row.new(["A"], ["foo"])
dupped_row = row.dup dupped_row = row.dup
dupped_row["A"] = "bar"
assert_equal(["foo", "bar"],
[row["A"], dupped_row["A"]])
dupped_row.delete("A") dupped_row.delete("A")
assert_equal(["foo", nil], assert_equal(["foo", nil],
[row["A"], dupped_row["A"]]) [row["A"], dupped_row["A"]])