End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
CSV#readlines
readlines
lib/csv.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.2.0/lib/csv.rb, line 2594
def read
rows = to_a
if parser.use_headers?
Table.new(rows, headers: parser.headers)
else
rows
end
end
Alias of #read
Forms the remaining rows from self into:
-
A
CSV::Tableobject, if headers are in use. -
An Array of Arrays, otherwise.
The data source must be opened for reading.
Without headers:
Example
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string) csv = CSV.open(path) csv.read # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
With headers:
Example
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string) csv = CSV.open(path, headers: true) csv.read # => #<CSV::Table mode:col_or_row row_count:4>
Raises an exception if the source is not opened for reading:
Example
string = "foo,0\nbar,1\nbaz,2\n" csv = CSV.new(string) csv.close # Raises IOError (not opened for reading) csv.read