@@ -143,7 +143,7 @@ class CSV
143
143
# table['Name'] # => ["Foo", "Bar", "Baz"]
144
144
class Table
145
145
# :call-seq:
146
- # CSV::Table.new(array_of_rows, headers = nil)
146
+ # CSV::Table.new(array_of_rows, headers = nil) -> csv_table
147
147
#
148
148
# Returns a new \CSV::Table object.
149
149
#
@@ -223,7 +223,7 @@ def initialize(array_of_rows, headers: nil)
223
223
def_delegators :@table , :empty? , :length , :size
224
224
225
225
# :call-seq:
226
- # table.by_col
226
+ # table.by_col -> table_dup
227
227
#
228
228
# Returns a duplicate of +self+, in column mode
229
229
# (see {Column Mode}[#class-CSV::Table-label-Column+Mode]):
@@ -244,7 +244,7 @@ def by_col
244
244
end
245
245
246
246
# :call-seq:
247
- # table.by_col!
247
+ # table.by_col! -> self
248
248
#
249
249
# Sets the mode for +self+ to column mode
250
250
# (see {Column Mode}[#class-CSV::Table-label-Column+Mode]); returns +self+:
@@ -261,7 +261,7 @@ def by_col!
261
261
end
262
262
263
263
# :call-seq:
264
- # table.by_col_or_row
264
+ # table.by_col_or_row -> table_dup
265
265
#
266
266
# Returns a duplicate of +self+, in mixed mode
267
267
# (see {Mixed Mode}[#class-CSV::Table-label-Mixed+Mode]):
@@ -282,7 +282,7 @@ def by_col_or_row
282
282
end
283
283
284
284
# :call-seq:
285
- # table.by_col_or_row!
285
+ # table.by_col_or_row! -> self
286
286
#
287
287
# Sets the mode for +self+ to mixed mode
288
288
# (see {Mixed Mode}[#class-CSV::Table-label-Mixed+Mode]); returns +self+:
@@ -299,7 +299,7 @@ def by_col_or_row!
299
299
end
300
300
301
301
# :call-seq:
302
- # table.by_row
302
+ # table.by_row -> table_dup
303
303
#
304
304
# Returns a duplicate of +self+, in row mode
305
305
# (see {Row Mode}[#class-CSV::Table-label-Row+Mode]):
@@ -320,7 +320,7 @@ def by_row
320
320
end
321
321
322
322
# :call-seq:
323
- # table.by_row!
323
+ # table.by_row! -> self
324
324
#
325
325
# Sets the mode for +self+ to row mode
326
326
# (see {Row Mode}[#class-CSV::Table-label-Row+Mode]); returns +self+:
@@ -337,7 +337,7 @@ def by_row!
337
337
end
338
338
339
339
# :call-seq:
340
- # table.headers
340
+ # table.headers -> array_of_headers
341
341
#
342
342
# Returns a new \Array containing the \String headers for the table.
343
343
#
@@ -396,8 +396,7 @@ def headers
396
396
# table[-4] # => nil
397
397
#
398
398
# Raises an exception if the access mode is <tt>:row</tt>
399
- # and +n+ is not an
400
- # {Integer-convertible object}[https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html#label-Integer-Convertible+Objects].
399
+ # and +n+ is not an \Integer:
401
400
# table.by_row! # => #<CSV::Table mode:row row_count:4>
402
401
# # Raises TypeError (no implicit conversion of String into Integer):
403
402
# table['Name']
@@ -856,6 +855,9 @@ def delete(*indexes_or_headers)
856
855
end
857
856
end
858
857
858
+ # :call-seq:
859
+ # table.delete_if {|row_or_column| ... } -> self
860
+ #
859
861
# Removes rows or columns for which the block returns a truthy value;
860
862
# returns +self+.
861
863
#
@@ -899,6 +901,9 @@ def delete_if(&block)
899
901
900
902
include Enumerable
901
903
904
+ # :call-seq:
905
+ # table.each {|row_or_column| ... ) -> self
906
+ #
902
907
# Calls the block with each row or column; returns +self+.
903
908
#
904
909
# When the access mode is <tt>:row</tt> or <tt>:col_or_row</tt>,
@@ -935,6 +940,9 @@ def each(&block)
935
940
self # for chaining
936
941
end
937
942
943
+ # :call-seq:
944
+ # table == other_table -> true or false
945
+ #
938
946
# Returns +true+ if all each row of +self+ <tt>==</tt>
939
947
# the corresponding row of +other_table+, otherwise, +false+.
940
948
#
@@ -958,10 +966,14 @@ def ==(other)
958
966
@table == other
959
967
end
960
968
969
+ # :call-seq:
970
+ # table.to_a -> array_of_arrays
961
971
#
962
- # Returns the table as an Array of Arrays. Headers will be the first row,
963
- # then all of the field rows will follow.
964
- #
972
+ # Returns the table as an \Array of \Arrays;
973
+ # the headers are in the first row:
974
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
975
+ # table = CSV.parse(source, headers: true)
976
+ # table.to_a # => [["Name", "Value"], ["foo", "0"], ["bar", "1"], ["baz", "2"]]
965
977
def to_a
966
978
array = [ headers ]
967
979
@table . each do |row |
@@ -971,13 +983,20 @@ def to_a
971
983
array
972
984
end
973
985
986
+ # :call-seq:
987
+ # table.to_csv(**options) -> csv_string
974
988
#
975
- # Returns the table as a complete CSV String. Headers will be listed first,
976
- # then all of the field rows .
989
+ # Returns the table as \ CSV string.
990
+ # See {Options for Generating}[../CSV.html#class-CSV-label-Options+for+Generating] .
977
991
#
978
- # This method assumes you want the Table.headers(), unless you explicitly
979
- # pass <tt>:write_headers => false</tt>.
992
+ # Defaults option +write_headers+ to +true+:
993
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
994
+ # table = CSV.parse(source, headers: true)
995
+ # table.to_csv # => "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
980
996
#
997
+ # Omits the headers if option +write_headers+ is given as +false+
998
+ # (see {Option +write_headers+}[../CSV.html#class-CSV-label-Option+write_headers]):
999
+ # table.to_csv(write_headers: false) # => "foo,0\nbar,1\nbaz,2\n"
981
1000
def to_csv ( write_headers : true , **options )
982
1001
array = write_headers ? [ headers . to_csv ( **options ) ] : [ ]
983
1002
@table . each do |row |
@@ -1006,7 +1025,18 @@ def dig(index_or_header, *index_or_headers)
1006
1025
end
1007
1026
end
1008
1027
1009
- # Shows the mode and size of this table in a US-ASCII String.
1028
+ # :call-seq:
1029
+ # table.inspect => string
1030
+ #
1031
+ # Returns a <tt>US-ASCII</tt>-encoded \String showing table:
1032
+ # - Class: <tt>CSV::Table</tt>.
1033
+ # - Access mode: <tt>:row</tt>, <tt>:col</tt>, or <tt>:col_or_row</tt>.
1034
+ # - Size: Row count, including the header row.
1035
+ #
1036
+ # Example:
1037
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
1038
+ # table = CSV.parse(source, headers: true)
1039
+ # table.inspect # => "#<CSV::Table mode:col_or_row row_count:4>"
1010
1040
def inspect
1011
1041
"#<#{ self . class } mode:#{ @mode } row_count:#{ to_a . size } >" . encode ( "US-ASCII" )
1012
1042
end
0 commit comments