Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
CSV::Row#to_h
row.to_h → hash
lib/csv/row.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/csv/row.rb, line 653
def to_h
hash = {}
each do |key, _value|
hash[key] = self[key] unless hash.key?(key)
end
hash
end
Returns the new Hash formed by adding each header-value pair in self as a key-value pair in the Hash.
Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) row = table[0] row.to_h # => {"Name"=>"foo", "Value"=>"0"}
Header order is preserved, but repeated headers are ignored:
Example
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row.to_h # => {"Name"=>"Foo"}