Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

CSV::Table#each

table.each {|row_or_column| ... ) → self

Calls the block with each row or column; returns self.

When the access mode is :row or :col_or_row, calls the block with each CSV::Row object:

Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
table.by_row! # => #<CSV::Table mode:row row_count:4>
table.each {|row| p row }

Output:

Example
#<CSV::Row "Name":"foo" "Value":"0">
#<CSV::Row "Name":"bar" "Value":"1">
#<CSV::Row "Name":"baz" "Value":"2">

When the access mode is :col, calls the block with each column as a 2-element array containing the header and an Array of column fields:

Example
table.by_col! # => #<CSV::Table mode:col row_count:4>
table.each {|column_data| p column_data }

Output:

Example
["Name", ["foo", "bar", "baz"]]
["Value", ["0", "1", "2"]]

Returns a new Enumerator if no block is given:

Example
table.each # => #<Enumerator: #<CSV::Table mode:col row_count:4>:each>
See also #dig #headers #delete_if #inspect also in 3.3 · 3.1 · 3.0