From 31ccc233b1789f97dbf741c9e84b674af4a452d5 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Tue, 25 Aug 2020 21:23:07 -0500 Subject: [PATCH] [ruby/csv] Enhanced RDoc for Row#[]= (#170) https://github.com/ruby/csv/commit/744e83043f --- lib/csv/row.rb | 57 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/lib/csv/row.rb b/lib/csv/row.rb index f63ff23863..1dc8e58a8e 100644 --- a/lib/csv/row.rb +++ b/lib/csv/row.rb @@ -88,8 +88,6 @@ class CSV # field(header, offset) # # Returns the field value for the given +index+ or +header+. - # If an \Integer +offset+ is given, the first +offset+ columns are - # ignored. # # --- # @@ -209,17 +207,56 @@ class CSV # # :call-seq: - # []=( header, value ) - # []=( header, offset, value ) - # []=( index, value ) + # row[index] = value -> value + # row[header, offset] = value -> value + # row[header] = value -> value # - # Looks up the field by the semantics described in CSV::Row.field() and - # assigns the +value+. + # Assigns the field value for the given +index+ or +header+; + # returns +value+. # - # Assigning past the end of the row with an index will set all pairs between - # to [nil, nil]. Assigning to an unused header appends the new - # pair. + # --- # + # Assign field value by \Integer index: + # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" + # table = CSV.parse(source, headers: true) + # row = table[0] + # row[0] = 'Bat' + # row[1] = 3 + # row # => # + # + # Counts backward from the last column if +index+ is negative: + # row[-1] = 4 + # row[-2] = 'Bam' + # row # => # + # + # Extends the row with nil:nil if positive +index+ is not in the row: + # row[4] = 5 + # row # => # + # + # Raises IndexError if negative +index+ is too small (too far from zero). + # + # --- + # + # Assign field value by header (first found): + # source = "Name,Name,Name\nFoo,Bar,Baz\n" + # table = CSV.parse(source, headers: true) + # row = table[0] + # row['Name'] = 'Bat' + # row # => # + # + # Assign field value by header, ignoring +offset+ leading fields: + # source = "Name,Name,Name\nFoo,Bar,Baz\n" + # table = CSV.parse(source, headers: true) + # row = table[0] + # row['Name', 2] = 4 + # row # => # + # + # Append new field by (new) header: + # source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" + # table = CSV.parse(source, headers: true) + # row = table[0] + # row['New'] = 6 + # row# => # def []=(*args) value = args.pop