Skip to content

Commit 3283ef1

Browse files
BurdetteLamarkou
authored andcommitted
[ruby/csv] Enhanced RDoc for Table#[] (#162)
* Enhanced RDoc for Table#[] * Enhanced RDoc for Table#[] ruby/csv@5575ffc82e
1 parent 067b217 commit 3283ef1

File tree

1 file changed

+85
-8
lines changed

1 file changed

+85
-8
lines changed

lib/csv/table.rb

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,91 @@ def headers
144144
end
145145
end
146146

147-
#
148-
# In the default mixed mode, this method returns rows for index access and
149-
# columns for header access. You can force the index association by first
150-
# calling by_col!() or by_row!().
151-
#
152-
# Columns are returned as an Array of values. Altering that Array has no
153-
# effect on the table.
154-
#
147+
# :call-seq:
148+
# table[n] -> row
149+
# table[range] -> array_of_rows
150+
# table[header] -> array_of_fields
151+
#
152+
# Returns data from the table; does not modify the table.
153+
#
154+
# ---
155+
#
156+
# The expression <tt>table[n]</tt>, where +n+ is a non-negative \Integer,
157+
# returns the +n+th row of the table, if that row exists,
158+
# and if the access mode is <tt>:row</tt> or <tt>:col_or_row</tt>:
159+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
160+
# table = CSV.parse(source, headers: true)
161+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
162+
# table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
163+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
164+
# table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
165+
#
166+
# Counts backward from the last row if +n+ is negative:
167+
# table[-1] # => #<CSV::Row "Name":"baz" "Value":"2">
168+
#
169+
# Returns +nil+ if +n+ is too large or too small:
170+
# table[4] # => nil
171+
# table[-4] => nil
172+
#
173+
# Raises an exception if the access mode is <tt>:row</tt>
174+
# and +n+ is not an
175+
# {Integer-convertible object}[https://docs.ruby-lang.org/en/master/implicit_conversion_rdoc.html#label-Integer-Convertible+Objects].
176+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
177+
# # Raises TypeError (no implicit conversion of String into Integer):
178+
# table['Name']
179+
#
180+
# ---
181+
#
182+
# The expression <tt>table[range]</tt>, where +range+ is a Range object,
183+
# returns rows from the table, beginning at row <tt>range.first</tt>,
184+
# if those rows exist, and if the access mode is <tt>:row</tt> or <tt>:col_or_row</tt>:
185+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
186+
# table = CSV.parse(source, headers: true)
187+
# table.by_row! # => #<CSV::Table mode:row row_count:4>
188+
# rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
189+
# rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
190+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
191+
# rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1">
192+
# rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
193+
#
194+
# If there are too few rows, returns all from <tt>range.first</tt> to the end:
195+
# rows = table[1..50] # => #<CSV::Row "Name":"bar" "Value":"1">
196+
# rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
197+
#
198+
# Special case: if <tt>range.start == table.size</tt>, returns an empty \Array:
199+
# table[table.size..50] # => []
200+
#
201+
# If <tt>range.end</tt> is negative, calculates the ending index from the end:
202+
# rows = table[0..-1]
203+
# rows # => [#<CSV::Row "Name":"foo" "Value":"0">, #<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
204+
#
205+
# If <tt>range.start</tt> is negative, calculates the starting index from the end:
206+
# rows = table[-1..2]
207+
# rows # => [#<CSV::Row "Name":"baz" "Value":"2">]
208+
#
209+
# If <tt>range.start</tt> is larger than <tt>table.size</tt>, returns +nil+:
210+
# table[4..4] # => nil
211+
#
212+
# ---
213+
#
214+
# The expression <tt>table[header]</tt>, where +header+ is a \String,
215+
# returns column values (\Array of \Strings) if the column exists
216+
# and if the access mode is <tt>:col</tt> or <tt>:col_or_row</tt>:
217+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
218+
# table = CSV.parse(source, headers: true)
219+
# table.by_col! # => #<CSV::Table mode:col row_count:4>
220+
# table['Name'] # => ["foo", "bar", "baz"]
221+
# table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4>
222+
# col = table['Name']
223+
# col # => ["foo", "bar", "baz"]
224+
#
225+
# Modifying the returned column values does not modify the table:
226+
# col[0] = 'bat'
227+
# col # => ["bat", "bar", "baz"]
228+
# table['Name'] # => ["foo", "bar", "baz"]
229+
#
230+
# Returns an \Array of +nil+ values if there is no such column:
231+
# table['Nosuch'] # => [nil, nil, nil]
155232
def [](index_or_header)
156233
if @mode == :row or # by index
157234
(@mode == :col_or_row and (index_or_header.is_a?(Integer) or index_or_header.is_a?(Range)))

0 commit comments

Comments
 (0)