End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
CSV.foreach
# File tmp/rubies/ruby-3.0.5/lib/csv.rb, line 1203
def foreach(path, mode="r", **options, &block)
return to_enum(__method__, path, mode, **options) unless block_given?
open(path, mode, **options) do |csv|
csv.each(&block)
end
end
Calls the block with each row read from source path or io.
-
Argument
path, if given, must be the path to a file. -
Argument
ioshould be anIOobject that is:-
Open for reading; on return, the
IOobject will be closed. -
Positioned at the beginning. To position at the end, for appending, use method
CSV.generate. For any other positioning, pass a preset StringIO object instead.
-
-
Argument
mode, if given, must be a File mode See Open Mode. -
Arguments
**optionsmust be keyword options. See Options for Parsing. -
This method optionally accepts an additional
:encodingoption that you can use to specify theEncodingof the data read frompathorio. You must provide this unless your data is in the encoding given byEncoding::default_external. Parsing will use this to determine how to parse the data. You may provide a secondEncodingto have the data transcoded as it is read. For example,encoding: 'UTF-32BE:UTF-8'
would read
UTF-32BEdata from the file but transcode it toUTF-8before parsing.
Without Option headers
Without option headers, returns each row as an Array object.
These examples assume prior execution of:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Read rows from a file at path:
CSV.foreach(path) {|row| p row }
Output:
["foo", "0"] ["bar", "1"] ["baz", "2"]
Read rows from an IO object:
File.open(path) do |file| CSV.foreach(file) {|row| p row } end
Output:
["foo", "0"] ["bar", "1"] ["baz", "2"]
Returns a new Enumerator if no block given:
CSV.foreach(path) # => #<Enumerator: CSV:foreach("t.csv", "r")> CSV.foreach(File.open(path)) # => #<Enumerator: CSV:foreach(#<File:t.csv>, "r")>
Issues a warning if an encoding is unsupported:
CSV.foreach(File.open(path), encoding: 'foo:bar') {|row| }
Output:
warning: Unsupported encoding foo ignored warning: Unsupported encoding bar ignored
With Option headers
With option headers, returns each row as a CSV::Row object.
These examples assume prior execution of:
string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Read rows from a file at path:
CSV.foreach(path, headers: true) {|row| p row }
Output:
#<CSV::Row "Name":"foo" "Count":"0"> #<CSV::Row "Name":"bar" "Count":"1"> #<CSV::Row "Name":"baz" "Count":"2">
Read rows from an IO object:
File.open(path) do |file| CSV.foreach(file, headers: true) {|row| p row } end
Output:
#<CSV::Row "Name":"foo" "Count":"0"> #<CSV::Row "Name":"bar" "Count":"1"> #<CSV::Row "Name":"baz" "Count":"2">
Raises an exception if path is a String, but not the path to a readable file:
# Raises Errno::ENOENT (No such file or directory @ rb_sysopen - nosuch.csv): CSV.foreach('nosuch.csv') {|row| }
Raises an exception if io is an IO object, but not open for reading:
io = File.open(path, 'w') {|row| } # Raises TypeError (no implicit conversion of nil into String): CSV.foreach(io) {|row| }
Raises an exception if mode is invalid:
# Raises ArgumentError (invalid access mode nosuch): CSV.foreach(path, 'nosuch') {|row| }