summaryrefslogtreecommitdiff
path: root/doc/csv/options/parsing/converters.rdoc
blob: 993803c5d039655ccb95450c28440a8ce6d8dbf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
====== Option +converters+

Specifies a single field converter name or \Proc,
or an \Array of field converter names and Procs.

See {Field Converters}[#class-CSV-label-Field+Converters]

Default value:
  CSV::DEFAULT_OPTIONS.fetch(:converters) # => nil

The value may be a single field converter name:
  str = '1,2,3'
  # Without a converter
  ary = CSV.parse_line(str)
  ary # => ["1", "2", "3"]
  # With built-in converter :integer
  ary = CSV.parse_line(str, converters: :integer)
  ary # => [1, 2, 3]

The value may be an \Array of field converter names:
  str = '1,3.14159'
  # Without converters
  ary = CSV.parse_line(str)
  ary # => ["1", "3.14159"]
  # With built-in converters
  ary = CSV.parse_line(str, converters: [:integer, :float])
  ary # => [1, 3.14159]

The value may be a \Proc custom converter:
  str = ' foo  ,  bar  ,  baz  '
  # Without a converter
  ary = CSV.parse_line(str)
  ary # => [" foo  ", "  bar  ", "  baz  "]
  # With a custom converter
  ary = CSV.parse_line(str, converters: proc {|field| field.strip })
  ary # => ["foo", "bar", "baz"]

See also {Custom Converters}[#class-CSV-label-Custom+Converters]

---

Raises an exception if the converter is not a converter name or a \Proc:
  str = 'foo,0'
  # Raises NoMethodError (undefined method `arity' for nil:NilClass)
  CSV.parse(str, converters: :foo)