Skip to content

Commit f3e9586

Browse files
Clarify and correct RDoc for converters (#178)
1 parent aea896f commit f3e9586

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

lib/csv.rb

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,15 @@
679679
#
680680
# You can define a custom field converter:
681681
# strip_converter = proc {|field| field.strip }
682-
# Add it to the \Converters \Hash:
683-
# CSV::Converters[:strip] = strip_converter
684-
# Use it by name:
685682
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
686683
# array = CSV.parse(string, converters: strip_converter)
687684
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
685+
# You can register the converter in \Converters \Hash,
686+
# which allows you to refer to it by name:
687+
# CSV::Converters[:strip] = strip_converter
688+
# string = " foo , 0 \n bar , 1 \n baz , 2 \n"
689+
# array = CSV.parse(string, converters: :strip)
690+
# array # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
688691
#
689692
# ==== Header \Converters
690693
#
@@ -742,13 +745,16 @@
742745
#
743746
# You can define a custom header converter:
744747
# upcase_converter = proc {|header| header.upcase }
745-
# Add it to the \HeaderConverters \Hash:
746-
# CSV::HeaderConverters[:upcase] = upcase_converter
747-
# Use it by name:
748748
# string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
749-
# table = CSV.parse(string, headers: true, converters: upcase_converter)
749+
# table = CSV.parse(string, headers: true, header_converters: upcase_converter)
750750
# table # => #<CSV::Table mode:col_or_row row_count:4>
751-
# table.headers # => ["Name", "Value"]
751+
# table.headers # => ["NAME", "VALUE"]
752+
# You can register the converter in \HeaderConverters \Hash,
753+
# which allows you to refer to it by name:
754+
# CSV::HeaderConverters[:upcase] = upcase_converter
755+
# table = CSV.parse(string, headers: true, header_converters: :upcase)
756+
# table # => #<CSV::Table mode:col_or_row row_count:4>
757+
# table.headers # => ["NAME", "VALUE"]
752758
#
753759
# ===== Write \Converters
754760
#
@@ -757,23 +763,23 @@
757763
# its return value becomes the new value for the field.
758764
# A converter might, for example, strip whitespace from a field.
759765
#
760-
# - Using no write converter (all fields unmodified):
761-
# output_string = CSV.generate do |csv|
762-
# csv << [' foo ', 0]
763-
# csv << [' bar ', 1]
764-
# csv << [' baz ', 2]
765-
# end
766-
# output_string # => " foo ,0\n bar ,1\n baz ,2\n"
767-
# - Using option +write_converters+:
768-
# strip_converter = proc {|field| field.respond_to?(:strip) ? field.strip : field }
769-
# upcase_converter = proc {|field| field.respond_to?(:upcase) ? field.upcase : field }
770-
# converters = [strip_converter, upcase_converter]
771-
# output_string = CSV.generate(write_converters: converters) do |csv|
772-
# csv << [' foo ', 0]
773-
# csv << [' bar ', 1]
774-
# csv << [' baz ', 2]
775-
# end
776-
# output_string # => "FOO,0\nBAR,1\nBAZ,2\n"
766+
# Using no write converter (all fields unmodified):
767+
# output_string = CSV.generate do |csv|
768+
# csv << [' foo ', 0]
769+
# csv << [' bar ', 1]
770+
# csv << [' baz ', 2]
771+
# end
772+
# output_string # => " foo ,0\n bar ,1\n baz ,2\n"
773+
# Using option +write_converters+ with two custom write converters:
774+
# strip_converter = proc {|field| field.respond_to?(:strip) ? field.strip : field }
775+
# upcase_converter = proc {|field| field.respond_to?(:upcase) ? field.upcase : field }
776+
# write_converters = [strip_converter, upcase_converter]
777+
# output_string = CSV.generate(write_converters: write_converters) do |csv|
778+
# csv << [' foo ', 0]
779+
# csv << [' bar ', 1]
780+
# csv << [' baz ', 2]
781+
# end
782+
# output_string # => "FOO,0\nBAR,1\nBAZ,2\n"
777783
#
778784
# === Character Encodings (M17n or Multilingualization)
779785
#

0 commit comments

Comments
 (0)