Skip to content

Commit d91ae93

Browse files
Enhanced RDoc for CSV::Row (#172)
1 parent cced8d8 commit d91ae93

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

lib/csv/row.rb

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class CSV
7979
#
8080
class Row
8181
# :call-seq:
82-
# CSV::Row.new(headers, fields, header_row = false)
82+
# CSV::Row.new(headers, fields, header_row = false) -> csv_row
8383
#
8484
# Returns the new \CSV::Row instance constructed from
8585
# arguments +headers+ and +fields+; both should be Arrays;
@@ -150,7 +150,7 @@ def field_row?
150150
end
151151

152152
# :call-seq:
153-
# row.headers
153+
# row.headers -> array_of_headers
154154
#
155155
# Returns the headers for this row:
156156
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
@@ -162,9 +162,9 @@ def headers
162162
end
163163

164164
# :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
168168
#
169169
# Returns the field value for the given +index+ or +header+.
170170
#
@@ -216,9 +216,9 @@ def field(header_or_index, minimum_index = 0)
216216

217217
#
218218
# :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
222222
#
223223
# Returns the field value as specified by +header+.
224224
#
@@ -272,7 +272,7 @@ def fetch(header, *varargs)
272272
end
273273

274274
# :call-seq:
275-
# row.has_key?(header)
275+
# row.has_key?(header) -> true or false
276276
#
277277
# Returns +true+ if there is a field with the given +header+,
278278
# +false+ otherwise.
@@ -399,7 +399,7 @@ def <<(arg)
399399
end
400400

401401
# :call-seq:
402-
# row.push(*values) ->self
402+
# row.push(*values) -> self
403403
#
404404
# Appends each of the given +values+ to +self+ as a field; returns +self+:
405405
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
@@ -482,7 +482,7 @@ def delete_if(&block)
482482
end
483483

484484
# :call-seq:
485-
# self.fields(*specifiers)
485+
# self.fields(*specifiers) -> array_of_fields
486486
#
487487
# Returns field values per the given +specifiers+, which may be any mixture of:
488488
# - \Integer index.
@@ -550,40 +550,63 @@ def fields(*headers_and_or_indices)
550550
end
551551
alias_method :values_at, :fields
552552

553-
#
554553
# :call-seq:
555-
# index( header )
556-
# index( header, offset )
554+
# index(header) -> index
555+
# index(header, offset) -> index
557556
#
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+.
561559
#
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
562573
def index(header, minimum_index = 0)
563574
# find the pair
564575
index = headers[minimum_index..-1].index(header)
565576
# return the index at the right offset, if we found one
566577
index.nil? ? nil : index + minimum_index
567578
end
568579

580+
# :call-seq:
581+
# row.field?(value) -> true or false
569582
#
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
573589
def field?(data)
574590
fields.include? data
575591
end
576592

577593
include Enumerable
578594

595+
# :call-seq:
596+
# row.each {|header, value| ... } -> self
579597
#
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"]
586607
#
608+
# If no block is given, returns a new Enumerator:
609+
# row.each # => #<Enumerator: #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":"Baz">:each>
587610
def each(&block)
588611
return enum_for(__method__) { size } unless block_given?
589612

@@ -594,10 +617,19 @@ def each(&block)
594617

595618
alias_method :each_pair, :each
596619

620+
# :call-seq:
621+
# row == other -> true or false
597622
#
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
601633
def ==(other)
602634
return @row == other.row if other.is_a? CSV::Row
603635
@row == other

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy