Skip to content

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

class method

CSV.generate_lines

CSV.generate_lines(rows)
CSV.generate_lines(rows, **options)

Returns the String created by generating CSV from using the specified options.

Argument rows must be an Array of row. Row is Array of String or CSV::Row.

Special options:

  • Option :row_sep defaults to "\n" on Ruby 3.0 or later and $INPUT_RECORD_SEPARATOR ($/) otherwise.:

    Example
    $INPUT_RECORD_SEPARATOR # => "\n"
    
  • This method accepts an additional option, :encoding, which sets the base Encoding for the output. This method will try to guess your Encoding from the first non-nil field in row, if possible, but you may need to use this parameter as a backup plan.

For other options, see Options for Generating.


Returns the String generated from an

Example
CSV.generate_lines(['foo', '0'], ['bar', '1'], ['baz', '2']) # => "foo,0\nbar,1\nbaz.2\n"

Raises an exception

Example
# Raises NoMethodError (undefined method `find' for :foo:Symbol)
CSV.generate_lines(:foo)