Method: CSV#shift
- Defined in:
- lib/csv.rb
#shift ⇒ Object Also known as: gets, readline
:call-seq:
csv.shift -> array, csv_row, or nil
Returns the next row of data as:
-
An Array if no headers are used.
-
A CSV::Row object if headers are used.
The data source must be opened for reading.
Without headers:
string = "foo,0\nbar,1\nbaz,2\n"
csv = CSV.new(string)
csv.shift # => ["foo", "0"]
csv.shift # => ["bar", "1"]
csv.shift # => ["baz", "2"]
csv.shift # => nil
With headers:
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
csv = CSV.new(string, headers: true)
csv.shift # => #<CSV::Row "Name":"foo" "Value":"0">
csv.shift # => #<CSV::Row "Name":"bar" "Value":"1">
csv.shift # => #<CSV::Row "Name":"baz" "Value":"2">
csv.shift # => nil
Raises an exception if the source is not opened for reading:
string = "foo,0\nbar,1\nbaz,2\n"
csv = CSV.new(string)
csv.close
# Raises IOError (not opened for reading)
csv.shift
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 |
# File 'lib/csv.rb', line 2668 def shift if @eof_error eof_error, @eof_error = @eof_error, nil raise eof_error end begin parser_enumerator.next rescue StopIteration nil end end |