@@ -832,7 +832,7 @@ def filter(input=nil, output=nil, **options)
832
832
# Returns an integer, or, if there were no rows, +nil+.
833
833
#
834
834
# * Argument +path+, if given, must be the path to a file.
835
- # * Argument +io+, if given, must be an \IO object opened for reading.
835
+ # :include: ../doc/argument_io.rdoc
836
836
# * Argument +mode+, if given, must be a \File mode
837
837
# See {Open Mode}[IO.html#method-c-new-label-Open+Mode].
838
838
# * Arguments <tt>**options</tt> must be keyword options.
@@ -854,7 +854,6 @@ def filter(input=nil, output=nil, **options)
854
854
# string = "foo,0\nbar,1\nbaz,2\n"
855
855
# path = 't.csv'
856
856
# File.write(path, string)
857
- # io = File.open(path)
858
857
#
859
858
# Read rows from a file at +path+:
860
859
# CSV.foreach(path) {|row| p row } # => 21
@@ -864,18 +863,18 @@ def filter(input=nil, output=nil, **options)
864
863
# ["baz", "2"]
865
864
#
866
865
# Read rows from an \IO object:
867
- # CSV.foreach(io ) {|row| p row } # => 21
866
+ # CSV.foreach(File.open(path) ) {|row| p row } # => 21
868
867
# Output:
869
868
# ["foo", "0"]
870
869
# ["bar", "1"]
871
870
# ["baz", "2"]
872
871
#
873
872
# Returns a new \Enumerator if no block given:
874
873
# CSV.foreach(path) # => #<Enumerator: CSV:foreach("t.csv", "r")>
875
- # CSV.foreach(io ) # => #<Enumerator: CSV:foreach(#<File:t.csv>, "r")>
874
+ # CSV.foreach(File.open(path) ) # => #<Enumerator: CSV:foreach(#<File:t.csv>, "r")>
876
875
#
877
876
# Issues a warning if an encoding is unsupported:
878
- # CSV.foreach(io , encoding: 'foo:bar') {|row| } # => 21
877
+ # CSV.foreach(File.open(path) , encoding: 'foo:bar') {|row| } # => 21
879
878
# Output:
880
879
# warning: Unsupported encoding foo ignored
881
880
# warning: Unsupported encoding bar ignored
@@ -892,8 +891,8 @@ def filter(input=nil, output=nil, **options)
892
891
# CSV.foreach(io) {|row| }
893
892
#
894
893
# Raises an exception if +mode+ is invalid:
895
- # # Raises IOError (not opened for reading ):
896
- # CSV.foreach(path, 'w ') {|row| }
894
+ # # Raises ArgumentError (invalid access mode nosuch ):
895
+ # CSV.foreach(path, 'nosuch ') {|row| }
897
896
#
898
897
def foreach ( path , mode = "r" , **options , &block )
899
898
return to_enum ( __method__ , path , mode , **options ) unless block_given?
@@ -1022,10 +1021,10 @@ def generate_line(row, **options)
1022
1021
1023
1022
#
1024
1023
# :call-seq:
1025
- # open( filename , mode = "rb", **options ) { |faster_csv| ... }
1026
- # open( filename , **options ) { |faster_csv| ... }
1027
- # open( filename , mode = "rb", **options )
1028
- # open( filename , **options )
1024
+ # open(file_path , mode = "rb", **options ) -> new_csv
1025
+ # open(io, mode = "rb" , **options ) -> new_csv
1026
+ # open(file_path , mode = "rb", **options ) { |csv| ... } -> new_csv
1027
+ # open(io, mode = "rb" , **options ) { |csv| ... } -> new_csv
1029
1028
#
1030
1029
# possible options elements:
1031
1030
# hash form:
@@ -1034,28 +1033,63 @@ def generate_line(row, **options)
1034
1033
# :undef => :replace # replace undefined conversion
1035
1034
# :replace => string # replacement string ("?" or "\uFFFD" if not specified)
1036
1035
#
1037
- # This method opens an IO object, and wraps that with CSV. This is intended
1038
- # as the primary interface for writing a CSV file.
1036
+ # * Argument +path+, if given, must be the path to a file.
1037
+ # :include: ../doc/argument_io.rdoc
1038
+ # * Argument +mode+, if given, must be a \File mode
1039
+ # See {Open Mode}[IO.html#method-c-new-label-Open+Mode].
1040
+ # * Arguments <tt>**options</tt> must be keyword options.
1041
+ # See {Options for Generating}[#class-CSV-label-Options+for+Generating].
1042
+ # * This method optionally accepts an additional <tt>:encoding</tt> option
1043
+ # that you can use to specify the Encoding of the data read from +path+ or +io+.
1044
+ # You must provide this unless your data is in the encoding
1045
+ # given by <tt>Encoding::default_external</tt>.
1046
+ # Parsing will use this to determine how to parse the data.
1047
+ # You may provide a second Encoding to
1048
+ # have the data transcoded as it is read. For example,
1049
+ # encoding: 'UTF-32BE:UTF-8'
1050
+ # would read +UTF-32BE+ data from the file
1051
+ # but transcode it to +UTF-8+ before parsing.
1039
1052
#
1040
- # You must pass a +filename+ and may optionally add a +mode+ for Ruby's
1041
- # open().
1053
+ # ---
1042
1054
#
1043
- # See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
1055
+ # These examples assume prior execution of:
1056
+ # string = "foo,0\nbar,1\nbaz,2\n"
1057
+ # path = 't.csv'
1058
+ # File.write(path, string)
1059
+ #
1060
+ # ---
1061
+ #
1062
+ # With no block given, returns a new \CSV object.
1063
+ #
1064
+ # Create a \CSV object using a file path:
1065
+ # csv = CSV.open(path)
1066
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1044
1067
#
1045
- # This method works like Ruby's open() call, in that it will pass a CSV object
1046
- # to a provided block and close it when the block terminates, or it will
1047
- # return the CSV object when no block is provided. (*Note*: This is different
1048
- # from the Ruby 1.8 CSV library which passed rows to the block. Use
1049
- # CSV::foreach() for that behavior.)
1068
+ # Create a \CSV object using an open \File:
1069
+ # csv = CSV.open(File.open(path))
1070
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1071
+ #
1072
+ # ---
1073
+ #
1074
+ # With a block given, calls the block with the created \CSV object:
1075
+ #
1076
+ # Using a file path:
1077
+ # csv = CSV.open(path) {|csv| p csv}
1078
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1079
+ # Output:
1080
+ # #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1081
+ #
1082
+ # Using an open \File:
1083
+ # csv = CSV.open(File.open(path)) {|csv| p csv}
1084
+ # csv # => #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1085
+ # Output:
1086
+ # #<CSV io_type:File io_path:"t.csv" encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\r\n" quote_char:"\"">
1050
1087
#
1051
- # You must provide a +mode+ with an embedded Encoding designator unless your
1052
- # data is in Encoding::default_external(). CSV will check the Encoding of the
1053
- # underlying IO object (set by the +mode+ you pass) to determine how to parse
1054
- # the data. You may provide a second Encoding to have the data transcoded as
1055
- # it is read just as you can with a normal call to IO::open(). For example,
1056
- # <tt>"rb:UTF-32BE:UTF-8"</tt> would read UTF-32BE data from the file but
1057
- # transcode it to UTF-8 before CSV parses it.
1088
+ # ---
1058
1089
#
1090
+ # Raises an exception if the argument is not a \String object or \IO object:
1091
+ # # Raises TypeError (no implicit conversion of Symbol into String)
1092
+ # CSV.open(:foo)
1059
1093
def open ( filename , mode = "r" , **options )
1060
1094
# wrap a File opened with the remaining +args+ with no newline
1061
1095
# decorator
@@ -1093,16 +1127,60 @@ def open(filename, mode="r", **options)
1093
1127
1094
1128
#
1095
1129
# :call-seq:
1096
- # parse( str, **options ) { |row| ... }
1097
- # parse( str, **options )
1130
+ # parse(string) -> array_of_arrays
1131
+ # parse(io) -> array_of_arrays
1132
+ # parse(string, **options) {|row| ... } -> integer
1133
+ # parse(io, **options) {|row| ... } -> integer
1098
1134
#
1099
- # This method can be used to easily parse CSV out of a String. You may either
1100
- # provide a +block+ which will be called with each row of the String in turn,
1101
- # or just use the returned Array of Arrays (when no +block+ is given).
1135
+ # Parses +string+ or +io+ using the specified +options+.
1102
1136
#
1103
- # You pass your +str+ to read from, and an optional +options+.
1104
- # See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
1137
+ # - Argument +string+ should be a \String object;
1138
+ # it will be put into a new StringIO object positioned at the beginning.
1139
+ # :include: ../doc/argument_io.rdoc
1140
+ # - Argument +options+: see {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
1141
+ #
1142
+ # These examples assume prior execution of:
1143
+ # string = "foo,0\nbar,1\nbaz,2\n"
1144
+ # path = 't.csv'
1145
+ # File.write(path, string)
1146
+ #
1147
+ # ---
1148
+ #
1149
+ # With no block given, returns an \Array of Arrays formed from the source.
1150
+ #
1151
+ # Parse a \String:
1152
+ # a_of_a = CSV.parse(string)
1153
+ # a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1154
+ #
1155
+ # Parse an open \File:
1156
+ # a_of_a = CSV.parse(File.open(path))
1157
+ # a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
1158
+ #
1159
+ # ---
1160
+ #
1161
+ # With a block given, calls the block with each parsed row:
1162
+ #
1163
+ # Parse a \String:
1164
+ # CSV.parse(string) {|row| p row } # => 18
1165
+ #
1166
+ # Output:
1167
+ # ["foo", "0"]
1168
+ # ["bar", "1"]
1169
+ # ["baz", "2"]
1170
+ #
1171
+ # Parse an open \File:
1172
+ # CSV.parse(File.open(path)) {|row| p row } # => 18
1173
+ #
1174
+ # Output:
1175
+ # ["foo", "0"]
1176
+ # ["bar", "1"]
1177
+ # ["baz", "2"]
1178
+ #
1179
+ # ---
1105
1180
#
1181
+ # Raises an exception if the argument is not a \String object or \IO object:
1182
+ # # Raises NoMethodError (undefined method `close' for :foo:Symbol)
1183
+ # CSV.parse(:foo)
1106
1184
def parse ( str , **options , &block )
1107
1185
csv = new ( str , **options )
1108
1186
@@ -1127,11 +1205,10 @@ def parse(str, **options, &block)
1127
1205
#
1128
1206
# - Argument +string+ should be a \String object;
1129
1207
# it will be put into a new StringIO object positioned at the beginning.
1130
- # - Argument +io+ should be an IO object, positioned at the beginning.
1208
+ # :include: ../doc/argument_io.rdoc
1131
1209
# To position at the end, for appending, use method CSV.generate.
1132
1210
# For any other positioning, pass a preset \StringIO object instead.
1133
- # - Argument +options+: see {Options for Generating}[#class-CSV-label-Options+for+Generating]
1134
- # For +options+, see {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
1211
+ # - Argument +options+: see {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
1135
1212
#
1136
1213
# ---
1137
1214
# Returns data from the first line from a String object:
@@ -1210,9 +1287,7 @@ def table(path, **options)
1210
1287
#
1211
1288
# - Argument +string+ should be a \String object;
1212
1289
# it will be put into a new StringIO object positioned at the beginning.
1213
- # - Argument +io+ should be an IO object, positioned at the beginning.
1214
- # To position at the end, for appending, use method CSV.generate.
1215
- # For any other positioning, pass a preset StringIO object instead.
1290
+ # :include: ../doc/argument_io.rdoc
1216
1291
# - Argument +options+: See:
1217
1292
# * {Options for Parsing}[#class-CSV-label-Options+for+Parsing]
1218
1293
# * {Options for Generating}[#class-CSV-label-Options+for+Generating]
0 commit comments