@@ -167,8 +167,6 @@ def headers
167
167
# field(header, offset)
168
168
#
169
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
170
#
173
171
# ---
174
172
#
@@ -288,17 +286,56 @@ def has_key?(header)
288
286
289
287
#
290
288
# :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
294
292
#
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+.
297
295
#
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
+ # ---
301
318
#
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>
302
339
def []=( *args )
303
340
value = args . pop
304
341
0 commit comments