|
679 | 679 | # |
680 | 680 | # You can define a custom field converter: |
681 | 681 | # strip_converter = proc {|field| field.strip } |
682 | | -# Add it to the \Converters \Hash: |
683 | | -# CSV::Converters[:strip] = strip_converter |
684 | | -# Use it by name: |
685 | 682 | # string = " foo , 0 \n bar , 1 \n baz , 2 \n" |
686 | 683 | # array = CSV.parse(string, converters: strip_converter) |
687 | 684 | # 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"]] |
688 | 691 | # |
689 | 692 | # ==== Header \Converters |
690 | 693 | # |
|
742 | 745 | # |
743 | 746 | # You can define a custom header converter: |
744 | 747 | # upcase_converter = proc {|header| header.upcase } |
745 | | -# Add it to the \HeaderConverters \Hash: |
746 | | -# CSV::HeaderConverters[:upcase] = upcase_converter |
747 | | -# Use it by name: |
748 | 748 | # 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) |
750 | 750 | # 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"] |
752 | 758 | # |
753 | 759 | # ===== Write \Converters |
754 | 760 | # |
|
757 | 763 | # its return value becomes the new value for the field. |
758 | 764 | # A converter might, for example, strip whitespace from a field. |
759 | 765 | # |
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" |
777 | 783 | # |
778 | 784 | # === Character Encodings (M17n or Multilingualization) |
779 | 785 | # |
|
0 commit comments