Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

CSV.generate_line

CSV.generate_line(ary)
CSV.generate_line(ary, **options)

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

Argument ary must be an Array.

Special options:

  • Option :row_sep defaults to "\n"> on Ruby 3.0 or later and <tt>$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 Array:

Example
CSV.generate_line(['foo', '0']) # => "foo,0\n"

Raises an exception if ary is not an Array:

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