Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/csv/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,10 @@ def ==(other)
def to_h
hash = {}
each do |key, _value|
hash[key] = self[key] unless hash.key?(key)
value = self[key]
key, value = yield(key, value) if block_given?

hash[key] = value unless hash.key?(key)
end
hash
end
Expand Down
6 changes: 6 additions & 0 deletions test/csv/test_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ def test_to_hash
end
end

def test_to_hash_with_block
row = CSV::Row.new(%w{A B C}, [1, 2, 3])
hash = row.to_hash { |k, v| [k, v ** 2] }
assert_equal({"A" => 1, "B" => 4, "C" => 9}, hash)
end

def test_to_csv
# normal conversion
assert_equal("1,2,3,4,\n", @row.to_csv)
Expand Down