Method: CSV#header_convert

Defined in:
lib/csv.rb

#header_convert(name = nil, &converter) ⇒ Object

The block need not return a String object:

csv = CSV.open(path, headers: true)
csv.header_convert {|header, field_info| header.to_sym }
table = csv.read
table.headers # => [:Name, :Value]

If converter_name is given, the block is not called:

csv = CSV.open(path, headers: true)
csv.header_convert(:downcase) {|header, field_info| fail 'Cannot happen' }
table = csv.read
table.headers # => ["name", "value"]

Raises a parse-time exception if converter_name is not the name of a built-in field converter:

csv = CSV.open(path, headers: true)
csv.header_convert(:nosuch)
# Raises NoMethodError (undefined method `arity' for nil:NilClass)
csv.read


2509
2510
2511
# File 'lib/csv.rb', line 2509

def header_convert(name = nil, &converter)
  header_fields_converter.add_converter(name, &converter)
end