Skip to content

Commit 744e830

Browse files
Enhanced RDoc for Row#[]= (#170)
1 parent 70ed12c commit 744e830

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

lib/csv/row.rb

+47-10
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ def headers
167167
# field(header, offset)
168168
#
169169
# Returns the field value for the given +index+ or +header+.
170-
# If an \Integer +offset+ is given, the first +offset+ columns are
171-
# ignored.
172170
#
173171
# ---
174172
#
@@ -288,17 +286,56 @@ def has_key?(header)
288286

289287
#
290288
# :call-seq:
291-
# []=( header, value )
292-
# []=( header, offset, value )
293-
# []=( index, value )
289+
# row[index] = value -> value
290+
# row[header, offset] = value -> value
291+
# row[header] = value -> value
294292
#
295-
# Looks up the field by the semantics described in CSV::Row.field() and
296-
# assigns the +value+.
293+
# Assigns the field value for the given +index+ or +header+;
294+
# returns +value+.
297295
#
298-
# Assigning past the end of the row with an index will set all pairs between
299-
# to <tt>[nil, nil]</tt>. Assigning to an unused header appends the new
300-
# pair.
296+
# ---
297+
#
298+
# Assign field value by \Integer index:
299+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
300+
# table = CSV.parse(source, headers: true)
301+
# row = table[0]
302+
# row[0] = 'Bat'
303+
# row[1] = 3
304+
# row # => #<CSV::Row "Name":"Bat" "Value":3>
305+
#
306+
# Counts backward from the last column if +index+ is negative:
307+
# row[-1] = 4
308+
# row[-2] = 'Bam'
309+
# row # => #<CSV::Row "Name":"Bam" "Value":4>
310+
#
311+
# Extends the row with <tt>nil:nil</tt> if positive +index+ is not in the row:
312+
# row[4] = 5
313+
# row # => #<CSV::Row "Name":"bad" "Value":4 nil:nil nil:nil nil:5>
314+
#
315+
# Raises IndexError if negative +index+ is too small (too far from zero).
316+
#
317+
# ---
301318
#
319+
# Assign field value by header (first found):
320+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
321+
# table = CSV.parse(source, headers: true)
322+
# row = table[0]
323+
# row['Name'] = 'Bat'
324+
# row # => #<CSV::Row "Name":"Bat" "Name":"Bar" "Name":"Baz">
325+
#
326+
# Assign field value by header, ignoring +offset+ leading fields:
327+
# source = "Name,Name,Name\nFoo,Bar,Baz\n"
328+
# table = CSV.parse(source, headers: true)
329+
# row = table[0]
330+
# row['Name', 2] = 4
331+
# row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":4>
332+
#
333+
# Append new field by (new) header:
334+
# source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
335+
# table = CSV.parse(source, headers: true)
336+
# row = table[0]
337+
# row['New'] = 6
338+
# row# => #<CSV::Row "Name":"foo" "Value":"0" "New":6>
302339
def []=(*args)
303340
value = args.pop
304341

0 commit comments

Comments
 (0)