ruby-changes:38848
From: hsbt <ko1@a...>
Date: Wed, 17 Jun 2015 10:59:01 +0900 (JST)
Subject: [ruby-changes:38848] hsbt:r50929 (trunk): * lib/csv.rb: accept to use Range object for row selection.
hsbt 2015-06-17 10:58:34 +0900 (Wed, 17 Jun 2015) New Revision: 50929 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50929 Log: * lib/csv.rb: accept to use Range object for row selection. [Feature #11267][ruby-dev:49091] Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_row.rb trunk/test/csv/test_table.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 50928) +++ ChangeLog (revision 50929) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Jun 17 10:57:28 2015 SHIBATA Hiroshi <hsbt@r...> + + * lib/csv.rb: accept to use Range object for row selection. + [Feature #11267][ruby-dev:49091] + Wed Jun 17 09:50:12 2015 Nobuyoshi Nakada <nobu@r...> * lib/rdoc/servlet.rb (documentation_search, root_search): Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 50928) +++ lib/csv.rb (revision 50929) @@ -284,11 +284,15 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L284 # def field(header_or_index, minimum_index = 0) # locate the pair - finder = header_or_index.is_a?(Integer) ? :[] : :assoc + finder = (header_or_index.is_a?(Integer) || header_or_index.is_a?(Range)) ? :[] : :assoc pair = @row[minimum_index..-1].send(finder, header_or_index) # return the field if we have a pair - pair.nil? ? nil : pair.last + if pair.nil? + nil + else + header_or_index.is_a?(Range) ? pair.map(&:last) : pair.last + end end alias_method :[], :field @@ -690,7 +694,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L694 # def [](index_or_header) if @mode == :row or # by index - (@mode == :col_or_row and index_or_header.is_a? Integer) + (@mode == :col_or_row and (index_or_header.is_a?(Integer) or index_or_header.is_a?(Range))) @table[index_or_header] else # by header @table.map { |row| row[index_or_header] } Index: test/csv/test_row.rb =================================================================== --- test/csv/test_row.rb (revision 50928) +++ test/csv/test_row.rb (revision 50929) @@ -64,6 +64,9 @@ class TestCSV::Row < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_row.rb#L64 # by index assert_equal(3, @row.field(2)) + # by range + assert_equal([2,3], @row.field(1..2)) + # missing assert_nil(@row.field("Missing")) assert_nil(@row.field(10)) Index: test/csv/test_table.rb =================================================================== --- test/csv/test_table.rb (revision 50928) +++ test/csv/test_table.rb (revision 50929) @@ -67,6 +67,9 @@ class TestCSV::Table < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_table.rb#L67 @rows.each_index { |i| assert_equal(@rows[i], @table[i]) } assert_equal(nil, @table[100]) # empty row + # by row with Range + assert_equal([@table[1], @table[2]], @table[1..2]) + # by col @rows.first.headers.each do |header| assert_equal(@rows.map { |row| row[header] }, @table[header]) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/