@@ -79,7 +79,7 @@ class CSV
79
79
#
80
80
class Row
81
81
# :call-seq:
82
- # CSV::Row.new(headers, fields, header_row = false)
82
+ # CSV::Row.new(headers, fields, header_row = false) -> csv_row
83
83
#
84
84
# Returns the new \CSV::Row instance constructed from
85
85
# arguments +headers+ and +fields+; both should be Arrays;
@@ -150,7 +150,7 @@ def field_row?
150
150
end
151
151
152
152
# :call-seq:
153
- # row.headers
153
+ # row.headers -> array_of_headers
154
154
#
155
155
# Returns the headers for this row:
156
156
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
@@ -162,9 +162,9 @@ def headers
162
162
end
163
163
164
164
# :call-seq:
165
- # field(index)
166
- # field(header)
167
- # field(header, offset)
165
+ # field(index) -> value
166
+ # field(header) -> value
167
+ # field(header, offset) -> value
168
168
#
169
169
# Returns the field value for the given +index+ or +header+.
170
170
#
@@ -216,9 +216,9 @@ def field(header_or_index, minimum_index = 0)
216
216
217
217
#
218
218
# :call-seq:
219
- # fetch(header)
220
- # fetch(header, default)
221
- # fetch(header) {|row| ... }
219
+ # fetch(header) -> value
220
+ # fetch(header, default) -> value
221
+ # fetch(header) {|row| ... } -> value
222
222
#
223
223
# Returns the field value as specified by +header+.
224
224
#
@@ -272,7 +272,7 @@ def fetch(header, *varargs)
272
272
end
273
273
274
274
# :call-seq:
275
- # row.has_key?(header)
275
+ # row.has_key?(header) -> true or false
276
276
#
277
277
# Returns +true+ if there is a field with the given +header+,
278
278
# +false+ otherwise.
@@ -399,7 +399,7 @@ def <<(arg)
399
399
end
400
400
401
401
# :call-seq:
402
- # row.push(*values) ->self
402
+ # row.push(*values) -> self
403
403
#
404
404
# Appends each of the given +values+ to +self+ as a field; returns +self+:
405
405
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
@@ -482,7 +482,7 @@ def delete_if(&block)
482
482
end
483
483
484
484
# :call-seq:
485
- # self.fields(*specifiers)
485
+ # self.fields(*specifiers) -> array_of_fields
486
486
#
487
487
# Returns field values per the given +specifiers+, which may be any mixture of:
488
488
# - \Integer index.
@@ -550,40 +550,63 @@ def fields(*headers_and_or_indices)
550
550
end
551
551
alias_method :values_at , :fields
552
552
553
- #
554
553
# :call-seq:
555
- # index( header )
556
- # index( header, offset )
554
+ # index(header) -> index
555
+ # index(header, offset) -> index
557
556
#
558
- # This method will return the index of a field with the provided +header+.
559
- # The +offset+ can be used to locate duplicate header names, as described in
560
- # CSV::Row.field().
557
+ # Returns the index for the given header, if it exists;
558
+ # otherwise returns +nil+.
561
559
#
560
+ # With the single argument +header+, returns the index
561
+ # of the first-found field with the given +header+:
562
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
563
+ # table = CSV.parse(source, headers: true)
564
+ # row = table[0]
565
+ # row.index('Name') # => 0
566
+ # row.index('NAME') # => nil
567
+ #
568
+ # With arguments +header+ and +offset+,
569
+ # returns the index of the first-found field with given +header+,
570
+ # but ignoring the first +offset+ fields:
571
+ # row.index('Name', 1) # => 1
572
+ # row.index('Name', 3) # => nil
562
573
def index ( header , minimum_index = 0 )
563
574
# find the pair
564
575
index = headers [ minimum_index ..-1 ] . index ( header )
565
576
# return the index at the right offset, if we found one
566
577
index . nil? ? nil : index + minimum_index
567
578
end
568
579
580
+ # :call-seq:
581
+ # row.field?(value) -> true or false
569
582
#
570
- # Returns +true+ if +data+ matches a field in this row, and +false+
571
- # otherwise.
572
- #
583
+ # Returns +true+ if +value+ is a field in this row, +false+ otherwise:
584
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
585
+ # table = CSV.parse(source, headers: true)
586
+ # row = table[0]
587
+ # row.field?('Bar') # => true
588
+ # row.field?('BAR') # => false
573
589
def field? ( data )
574
590
fields . include? data
575
591
end
576
592
577
593
include Enumerable
578
594
595
+ # :call-seq:
596
+ # row.each {|header, value| ... } -> self
579
597
#
580
- # Yields each pair of the row as header and field tuples (much like
581
- # iterating over a Hash). This method returns the row for chaining.
582
- #
583
- # If no block is given, an Enumerator is returned.
584
- #
585
- # Support for Enumerable.
598
+ # Calls the block with each header-value pair; returns +self+:
599
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
600
+ # table = CSV.parse(source, headers: true)
601
+ # row = table[0]
602
+ # row.each {|header, value| p [header, value] }
603
+ # Output:
604
+ # ["Name", "Foo"]
605
+ # ["Name", "Bar"]
606
+ # ["Name", "Baz"]
586
607
#
608
+ # If no block is given, returns a new Enumerator:
609
+ # row.each # => #<Enumerator: #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz">:each>
587
610
def each ( &block )
588
611
return enum_for ( __method__ ) { size } unless block_given?
589
612
@@ -594,10 +617,19 @@ def each(&block)
594
617
595
618
alias_method :each_pair , :each
596
619
620
+ # :call-seq:
621
+ # row == other -> true or false
597
622
#
598
- # Returns +true+ if this row contains the same headers and fields in the
599
- # same order as +other+.
600
- #
623
+ # Returns +true+ if +other+ is a /CSV::Row that has the same
624
+ # fields (headers and values) in the same order as +self+;
625
+ # otherwise returns +false+:
626
+ # source = "Name,Name,Name\nFoo,Bar,Baz\n"
627
+ # table = CSV.parse(source, headers: true)
628
+ # row = table[0]
629
+ # other_row = table[0]
630
+ # row == other_row # => true
631
+ # other_row = table[1]
632
+ # row == other_row # => false
601
633
def ==( other )
602
634
return @row == other . row if other . is_a? CSV ::Row
603
635
@row == other
0 commit comments