Skip to content

Commit 1d685ae

Browse files
Enhanced RDoc for CSV::Table (#166)
1 parent bce4b69 commit 1d685ae

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

doc/csv/options/common/col_sep.rdoc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,3 @@ Raises an exception if parsing with the empty \String:
5555
# Raises ArgumentError (:col_sep must be 1 or more characters: "")
5656
CSV.parse("foo0\nbar1\nbaz2\n", col_sep: col_sep)
5757

58-
Raises an exception if the given value is not String-convertible:
59-
col_sep = BasicObject.new
60-
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
61-
CSV.generate(line, col_sep: col_sep)
62-
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
63-
CSV.parse(str, col_sep: col_sep)

doc/csv/options/common/row_sep.rdoc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,3 @@ if any of the following is true:
8989
* The stream is only available for output.
9090

9191
Obviously, discovery takes a little time. Set manually if speed is important. Also note that IO objects should be opened in binary mode on Windows if this feature will be used as the line-ending translation can cause problems with resetting the document position to where it was before the read ahead.
92-
93-
---
94-
95-
Raises an exception if the given value is not String-convertible:
96-
row_sep = BasicObject.new
97-
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
98-
CSV.generate(ary, row_sep: row_sep)
99-
# Raises NoMethodError (undefined method `to_s' for #<BasicObject:>)
100-
CSV.parse(str, row_sep: row_sep)

doc/csv/options/generating/write_converters.rdoc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,3 @@ With two write converters (called in order):
2323
str # => "a,b,c\n"
2424

2525
See also {Write Converters}[#class-CSV-label-Write+Converters]
26-
27-
---
28-
29-
Raises an exception if the converter returns a value that is neither +nil+
30-
nor \String-convertible:
31-
bad_converter = proc {|field| BasicObject.new }
32-
# Raises NoMethodError (undefined method `is_a?' for #<BasicObject:>)
33-
CSV.generate_line(['a', 'b', 'c'], write_converters: bad_converter)

lib/csv/table.rb

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class CSV
143143
# table['Name'] # => ["Foo", "Bar", "Baz"]
144144
class Table
145145
# :call-seq:
146-
# CSV::Table.new(array_of_rows, headers = nil)
146+
# CSV::Table.new(array_of_rows, headers = nil) -> csv_table
147147
#
148148
# Returns a new \CSV::Table object.
149149
#
@@ -223,7 +223,7 @@ def initialize(array_of_rows, headers: nil)
223223
def_delegators :@table, :empty?, :length, :size
224224

225225
# :call-seq:
226-
# table.by_col
226+
# table.by_col -> table_dup
227227
#
228228
# Returns a duplicate of +self+, in column mode
229229
# (see {Column Mode}[#class-CSV::Table-label-Column+Mode]):
@@ -244,7 +244,7 @@ def by_col
244244
end
245245

246246
# :call-seq:
247-
# table.by_col!
247+
# table.by_col! -> self
248248
#
249249
# Sets the mode for +self+ to column mode
250250
# (see {Column Mode}[#class-CSV::Table-label-Column+Mode]); returns +self+:
@@ -261,7 +261,7 @@ def by_col!
261261
end
262262

263263
# :call-seq:
264-
# table.by_col_or_row
264+
# table.by_col_or_row -> table_dup
265265
#
266266
# Returns a duplicate of +self+, in mixed mode
267267
# (see {Mixed Mode}[#class-CSV::Table-label-Mixed+Mode]):
@@ -282,7 +282,7 @@ def by_col_or_row
282282
end
283283

284284
# :call-seq:
285-
# table.by_col_or_row!
285+
# table.by_col_or_row! -> self
286286
#
287287
# Sets the mode for +self+ to mixed mode
288288
# (see {Mixed Mode}[#class-CSV::Table-label-Mixed+Mode]); returns +self+:
@@ -299,7 +299,7 @@ def by_col_or_row!
299299
end
300300

301301
# :call-seq:
302-
# table.by_row
302+
# table.by_row -> table_dup
303303
#
304304
# Returns a duplicate of +self+, in row mode
305305
# (see {Row Mode}[#class-CSV::Table-label-Row+Mode]):
@@ -320,7 +320,7 @@ def by_row
320320
end
321321

322322
# :call-seq:
323-
# table.by_row!
323+
# table.by_row! -> self
324324
#
325325
# Sets the mode for +self+ to row mode
326326
# (see {Row Mode}[#class-CSV::Table-label-Row+Mode]); returns +self+:
@@ -337,7 +337,7 @@ def by_row!
337337
end
338338

339339
# :call-seq:
340-
# table.headers
340+
# table.headers -> array_of_headers
341341
#
342342
# Returns a new \Array containing the \String headers for the table.
343343
#
@@ -396,8 +396,7 @@ def headers
396396
# table[-4] # => nil
397397
#
398398
# 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:
401400
# table.by_row! # => #<CSV::Table mode:row row_count:4>
402401
# # Raises TypeError (no implicit conversion of String into Integer):
403402
# table['Name']
@@ -856,6 +855,9 @@ def delete(*indexes_or_headers)
856855
end
857856
end
858857

858+
# :call-seq:
859+
# table.delete_if {|row_or_column| ... } -> self
860+
#
859861
# Removes rows or columns for which the block returns a truthy value;
860862
# returns +self+.
861863
#
@@ -899,6 +901,9 @@ def delete_if(&block)
899901

900902
include Enumerable
901903

904+
# :call-seq:
905+
# table.each {|row_or_column| ... ) -> self
906+
#
902907
# Calls the block with each row or column; returns +self+.
903908
#
904909
# When the access mode is <tt>:row</tt> or <tt>:col_or_row</tt>,
@@ -935,6 +940,9 @@ def each(&block)
935940
self # for chaining
936941
end
937942

943+
# :call-seq:
944+
# table == other_table -> true or false
945+
#
938946
# Returns +true+ if all each row of +self+ <tt>==</tt>
939947
# the corresponding row of +other_table+, otherwise, +false+.
940948
#
@@ -958,10 +966,14 @@ def ==(other)
958966
@table == other
959967
end
960968

969+
# :call-seq:
970+
# table.to_a -> array_of_arrays
961971
#
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"]]
965977
def to_a
966978
array = [headers]
967979
@table.each do |row|
@@ -971,13 +983,20 @@ def to_a
971983
array
972984
end
973985

986+
# :call-seq:
987+
# table.to_csv(**options) -> csv_string
974988
#
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].
977991
#
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"
980996
#
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"
9811000
def to_csv(write_headers: true, **options)
9821001
array = write_headers ? [headers.to_csv(**options)] : []
9831002
@table.each do |row|
@@ -1006,7 +1025,18 @@ def dig(index_or_header, *index_or_headers)
10061025
end
10071026
end
10081027

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>"
10101040
def inspect
10111041
"#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII")
10121042
end

0 commit comments

Comments
 (0)