8000 Enhanced RDoc for Table::Row (#169) · ruby/csv@70ed12c · GitHub
[go: up one dir, main page]

Skip to content

Commit 70ed12c

Browse files
Enhanced RDoc for Table::Row (#169)
1 parent cf3b60d commit 70ed12c

File tree

1 file changed

+100
-26
lines changed

1 file changed

+100
-26
lines changed

lib/csv/row.rb

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,75 @@ def initialize_copy(other)
133133
super_return_value
134134
end
135135

136-
# Returns +true+ if this is a header row.
136+
# :call-seq:
137+
# row.header_row? -> true or false
138+
#
139+
# Returns +true+ if this is a header row, +false+ otherwise.
137140
def header_row?
138141
@header_row
139142
end
140143

141-
# Returns +true+ if this is a field row.
144+
# :call-seq:
145+
# row.field_row? -> true or false
146+
#
147+
# Returns +true+ if this is a field row, +false+ otherwise.
142148
def field_row?
143149
not header_row?
144150
end
145151

146-
# Returns the headers of this row.
152+
# :call-seq:
153+
# row.headers
154+
#
155+
# Returns the headers for this row:
156+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
157+
# table = CSV.parse(source, headers: true)
158+
# row = table.first
159+
# row.headers # => ["Name", "Value"]
147160
def headers
148161
@row.map(&:first)
149162
end
150163

151-
#
152164
# :call-seq:
153-
# field( header )
154-
# field( header, offset )
155-
# field( index )
156-
#
157-
# This method will return the field value by +header+ or +index+. If a field
158-
# is not found, +nil+ is returned.
159-
#
160-
# When provided, +offset+ ensures that a header match occurs on or later
161-
# than the +offset+ index. You can use this to find duplicate headers,
162-
# without resorting to hard-coding exact indices.
163-
#
165+
# field(index)
166+
# field(header)
167+
# field(header, offset)
168+
#
169+
# Returns the field value for the given +index+ or +header+.
170+
# If an \Integer +offset+ is given, the first +offset+ columns are
171+
# ignored.
172+
#
173+
# ---
174+
#
175+
# Fetch field value by \Integer index:
176+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
177+
# table = CSV.parse(source, headers: true)
178+
# row = table[0]
179+
# row.field(0) # => "foo"
180+
# row.field(1) # => "bar"
181+
#
182+
# Counts backward from the last column if +index+ is negative:
183+
# row.field(-1) # => "0"
184+
# row.field(-2) # => "foo"
185+
#
186+
# Returns +nil+ if +index+ is out of range:
187+
# row.field(2) # => nil
188+
# row.field(-3) # => nil
189+
#
190+
# ---
191+
#
192+
# Fetch field value by header (first found):
193+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
194+
# table = CSV.parse(source, headers: true)
195+
# row = table[0]
196+
# row.field('Name') # => "Foo"
197+
#
198+
# Fetch field value by header, ignoring +offset+ leading fields:
199+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
200+
# table = CSV.parse(source, headers: true)
201+
# row = table[0]
202+
# row.field('Name', 2) # => "Baz"
203+
#
204+
# Returns +nil+ if the header does not exist.
164205
def field(header_or_index, minimum_index = 0)
165206
# locate the pair
166207
finder = (header_or_index.is_a?(Integer) || header_or_index.is_a?(Range)) ? :[] : :assoc
@@ -177,16 +218,45 @@ def field(header_or_index, minimum_index = 0)
177218

178219
#
179220
# :call-seq:
180-
# fetch( header )
181-
# fetch( header ) { |row| ... }
182-
# fetch( header, default )
183-
#
184-
# This method will fetch the field value by +header+. It has the same
185-
# behavior as Hash#fetch: if there is a field with the given +header+, its
186-
# value is returned. Otherwise, if a block is given, it is yielded the
187-
# +header+ and its result is returned; if a +default+ is given as the
188-
# second argument, it is returned; otherwise a KeyError is raised.
189-
#
221+
# fetch(header)
222+
# fetch(header, default)
223+
# fetch(header) {|row| ... }
224+
#
225+
# Returns the field value as specified by +header+.
226+
#
227+
# ---
228+
#
229+
# With the single argument +header+, returns the field value
230+
# for that header (first found):
231+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
232+
# table = CSV.parse(source, headers: true)
233+
# row = table[0]
234+
# row.fetch('Name') # => "Foo"
235+
#
236+
# Raises exception +KeyError+ if the header does not exist.
237+
#
238+
# ---
239+
#
240+
# With arguments +header+ and +default+ given,
241+
# returns the field value for the header (first found)
242+
# if the header exists, otherwise returns +default+:
243+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
244+
# table = CSV.parse(source, headers: true)
245+
# row = table[0]
246+
# row.fetch('Name', '') # => "Foo"
247+
# row.fetch(:nosuch, '') # => ""
248+
#
249+
# ---
250+
#
251+
# With argument +header+ and a block given,
252+
# returns the field value for the header (first found)
253+
# if the header exists; otherwise calls the block
254+
# and returns its return value:
255+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
256+
# table = CSV.parse(source, headers: true)
257+
# row = table[0]
258+
# row.fetch('Name') {|header| fail 'Cannot happen' } # => "Foo"
259+
# row.fetch(:nosuch) {|header| "Header '#{header} not found'" } # => "Header 'nosuch not found'"
190260
def fetch(header, *varargs)
191261
raise ArgumentError, "Too many arguments" if varargs.length > 1
192262
pair = @row.assoc(header)
@@ -203,7 +273,11 @@ def fetch(header, *varargs)
203273
end
204274
end
205275

206-
# Returns +true+ if there is a field with the given +header+.
276+
# :call-seq:
277+
# row.has_key?(header)
278+
#
279+
# Returns +true+ if there is a field with the given +header+,
280+
# +false+ otherwise.
207281
def has_key?(header)
208282
!!@row.assoc(header)
209283
end

0 commit comments

Comments
 (0)
0