@@ -287,15 +287,58 @@ def []=(index_or_header, value)
287
287
end
288
288
end
289
289
290
+ # :call-seq:
291
+ # table.values_at(*indexes) -> array_of_rows
292
+ # table.values_at(*headers) -> array_of_columns_data
293
+ #
294
+ # If the access mode is <tt>:row</tt> or <tt>:col_or_row</tt>,
295
+ # and each argument is either an \Integer or a \Range,
296
+ # returns rows.
297
+ # Otherwise, returns columns data.
298
+ #
299
+ # In either case, the returned values are in the order
300
+ # specified by the arguments. Arguments may be repeated.
290
301
#
291
- # The mixed mode default is to treat a list of indices as row access,
292
- # returning the rows indicated. Anything else is considered columnar
293
- # access. For columnar access, the return set has an Array for each row
294
- # with the values indicated by the headers in each Array. You can force
295
- # column or row mode using by_col!() or by_row!().
302
+ # ---
296
303
#
297
- # You cannot mix column and row access .
304
+ # Returns rows as an \Array of \CSV::Row objects .
298
305
#
306
+ # No argument:
307
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
308
+ # table = CSV.parse(source, headers: true)
309
+ # table.values_at # => []
310
+ #
311
+ # One index:
312
+ # values = table.values_at(0)
313
+ # values # => [#<CSV::Row "Name":"foo" "Value":"0">]
314
+ #
315
+ # Two indexes:
316
+ # values = table.values_at(2, 0)
317
+ # values # => [#<CSV::Row "Name":"baz" "Value":"2">, #<CSV::Row "Name":"foo" "Value":"0">]
318
+ #
319
+ # One \Range:
320
+ # values = table.values_at(1..2)
321
+ # values # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
322
+ #
323
+ # \Ranges and indexes:
324
+ # values = table.values_at(0..1, 1..2, 0, 2)
325
+ # pp values
326
+ # Output:
327
+ # [#<CSV::Row "Name":"foo" "Value":"0">,
328
+ # #<CSV::Row "Name":"bar" "Value":"1">,
329
+ # #<CSV::Row "Name":"bar" "Value":"1">,
330
+ # #<CSV::Row "Name":"baz" "Value":"2">,
331
+ # #<CSV::Row "Name":"foo" "Value":"0">,
332
+ # #<CSV::Row "Name":"baz" "Value":"2">]
333
+ #
334
+ # ---
335
+ #
336
+ # Returns columns data as Arrays,
337
+ # each consisting of the specified columns data for that row:
338
+ # values = table.values_at('Name')
339
+ # values # => [["foo"], ["bar"], ["baz"]]
340
+ # values = table.values_at('Value', 'Name')
341
+ # values # => [["0", "foo"], ["1", "bar"], ["2", "baz"]]
299
342
def values_at ( *indices_or_headers )
300
343
if @mode == :row or # by indices
301
344
( @mode == :col_or_row and indices_or_headers . all? do |index |
@@ -310,13 +353,20 @@ def values_at(*indices_or_headers)
310
353
end
311
354
end
312
355
356
+ # :call-seq:
357
+ # table << row_or_array -> self
313
358
#
314
- # Adds a new row to the bottom end of this table. You can provide an Array,
315
- # which will be converted to a CSV::Row (inheriting the table's headers()),
316
- # or a CSV::Row.
317
- #
318
- # This method returns the table for chaining.
359
+ # If +row_or_array+ is a \CSV::Row object,
360
+ # it is appended to the table:
361
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
362
+ # table = CSV.parse(source, headers: true)
363
+ # table << CSV::Row.new(table.headers, ['bat', 3])
364
+ # table[3] # => #<CSV::Row "Name":"bat" "Value":3>
319
365
#
366
+ # If +row_or_array+ is an \Array, it is used to create a new
367
+ # \CSV::Row object which is then appended to the table:
368
+ # table << ['bam', 4]
369
+ # table[4] # => #<CSV::Row "Name":"bam" "Value":4>
320
370
def <<( row_or_array )
321
371
if row_or_array . is_a? Array # append Array
322
372
@table << Row . new ( headers , row_or_array )
@@ -328,12 +378,21 @@ def <<(row_or_array)
328
378
end
329
379
330
380
#
331
- # A shortcut for appending multiple rows. Equivalent to:
332
- #
333
- # rows.each { |row| self << row }
381
+ # :call-seq:
382
+ # table.push(*rows_or_arrays) -> self
334
383
#
335
- # This method returns the table for chaining.
384
+ # A shortcut for appending multiple rows. Equivalent to:
385
+ # rows.each {|row| self << row }
336
386
#
387
+ # Each argument may be either a \CSV::Row object or an \Array:
388
+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
389
+ # table = CSV.parse(source, headers: true)
390
+ # rows = [
391
+ # CSV::Row.new(table.headers, ['bat', 3]),
392
+ # ['bam', 4]
393
+ # ]
394
+ # table.push(*rows)
395
+ # table[3..4] # => [#<CSV::Row "Name":"bat" "Value":3>, #<CSV::Row "Name":"bam" "Value":4>]
337
396
def push ( *rows )
338
397
rows . each { |row | self << row }
339
398
0 commit comments