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#delete_if

table.delete_if {|row_or_column| ... } → self

Removes rows or columns for which the block returns a truthy value; returns self.

Removes rows 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.size # => 3
table.delete_if {|row| row['Name'].start_with?('b') }
table.size # => 1

Removes columns 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
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
table.by_col! # => #<CSV::Table mode:col row_count:4>
table.headers.size # => 2
table.delete_if {|column_data| column_data[1].include?('2') }
table.headers.size # => 1

Returns a new Enumerator if no block is given:

Example
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
table = CSV.parse(source, headers: true)
table.delete_if # => #<Enumerator: #<CSV::Table mode:col_or_row row_count:4>:delete_if>
See also #delete #dig #by_row! #each also in 3.3 · 3.1 · 3.0