@@ -680,15 +680,58 @@ def []=(index_or_header, value)
680680 end
681681 end
682682
683+ # :call-seq:
684+ # table.values_at(*indexes) -> array_of_rows
685+ # table.values_at(*headers) -> array_of_columns_data
686+ #
687+ # If the access mode is <tt>:row</tt> or <tt>:col_or_row</tt>,
688+ # and each argument is either an \Integer or a \Range,
689+ # returns rows.
690+ # Otherwise, returns columns data.
691+ #
692+ # In either case, the returned values are in the order
693+ # specified by the arguments. Arguments may be repeated.
694+ #
695+ # ---
696+ #
697+ # Returns rows as an \Array of \CSV::Row objects.
683698 #
684- # The mixed mode default is to treat a list of indices as row access,
685- # returning the rows indicated. Anything else is considered columnar
686- # access. For columnar access, the return set has an Array for each row
687- # with the values indicated by the headers in each Array. You can force
688- # column or row mode using by_col!() or by_row!().
699+ # No argument:
700+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
701+ # table = CSV.parse(source, headers: true)
702+ # table.values_at # => []
703+ #
704+ # One index:
705+ # values = table.values_at(0)
706+ # values # => [#<CSV::Row "Name":"foo" "Value":"0">]
707+ #
708+ # Two indexes:
709+ # values = table.values_at(2, 0)
710+ # values # => [#<CSV::Row "Name":"baz" "Value":"2">, #<CSV::Row "Name":"foo" "Value":"0">]
711+ #
712+ # One \Range:
713+ # values = table.values_at(1..2)
714+ # values # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
715+ #
716+ # \Ranges and indexes:
717+ # values = table.values_at(0..1, 1..2, 0, 2)
718+ # pp values
719+ # Output:
720+ # [#<CSV::Row "Name":"foo" "Value":"0">,
721+ # #<CSV::Row "Name":"bar" "Value":"1">,
722+ # #<CSV::Row "Name":"bar" "Value":"1">,
723+ # #<CSV::Row "Name":"baz" "Value":"2">,
724+ # #<CSV::Row "Name":"foo" "Value":"0">,
725+ # #<CSV::Row "Name":"baz" "Value":"2">]
689726 #
690- # You cannot mix column and row access.
727+ # ---
691728 #
729+ # Returns columns data as Arrays,
730+ # each consisting of the specified columns data for that row:
731+ # values = table.values_at('Name')
732+ # values # => [["foo"], ["bar"], ["baz"]]
733+ # values = table.values_at('Value', 'Name')
734+ # values # => [["0", "foo"], ["1", "bar"], ["2", "baz"]]
692735 def values_at ( *indices_or_headers )
693736 if @mode == :row or # by indices
694737 ( @mode == :col_or_row and indices_or_headers . all? do |index |
@@ -703,13 +746,20 @@ def values_at(*indices_or_headers)
703746 end
704747 end
705748
749+ # :call-seq:
750+ # table << row_or_array -> self
706751 #
707- # Adds a new row to the bottom end of this table. You can provide an Array,
708- # which will be converted to a CSV::Row (inheriting the table's headers()),
709- # or a CSV::Row.
710- #
711- # This method returns the table for chaining.
752+ # If +row_or_array+ is a \CSV::Row object,
753+ # it is appended to the table:
754+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
755+ # table = CSV.parse(source, headers: true)
756+ # table << CSV::Row.new(table.headers, ['bat', 3])
757+ # table[3] # => #<CSV::Row "Name":"bat" "Value":3>
712758 #
759+ # If +row_or_array+ is an \Array, it is used to create a new
760+ # \CSV::Row object which is then appended to the table:
761+ # table << ['bam', 4]
762+ # table[4] # => #<CSV::Row "Name":"bam" "Value":4>
713763 def <<( row_or_array )
714764 if row_or_array . is_a? Array # append Array
715765 @table << Row . new ( headers , row_or_array )
@@ -721,12 +771,21 @@ def <<(row_or_array)
721771 end
722772
723773 #
724- # A shortcut for appending multiple rows. Equivalent to:
725- #
726- # rows.each { |row| self << row }
774+ # :call-seq:
775+ # table.push(*rows_or_arrays) -> self
727776 #
728- # This method returns the table for chaining.
777+ # A shortcut for appending multiple rows. Equivalent to:
778+ # rows.each {|row| self << row }
729779 #
780+ # Each argument may be either a \CSV::Row object or an \Array:
781+ # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
782+ # table = CSV.parse(source, headers: true)
783+ # rows = [
784+ # CSV::Row.new(table.headers, ['bat', 3]),
785+ # ['bam', 4]
786+ # ]
787+ # table.push(*rows)
788+ # table[3..4] # => [#<CSV::Row "Name":"bat" "Value":3>, #<CSV::Row "Name":"bam" "Value":4>]
730789 def push ( *rows )
731790 rows . each { |row | self << row }
732791
0 commit comments