aboutsummaryrefslogtreecommitdiffstats
path: root/lib/csv.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-06-17 01:58:34 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-06-17 01:58:34 +0000
commit0d92eb44e79d1a216c728a5462180b7f6cc202cd (patch)
tree13e1ec6d009725037a71f557c4492d49609d0e05 /lib/csv.rb
parent71a8a86f10cfc7ab98c1ab82376955c951c9fc67 (diff)
downloadruby-0d92eb44e79d1a216c728a5462180b7f6cc202cd.tar.gz
* lib/csv.rb: accept to use Range object for row selection.
[Feature #11267][ruby-dev:49091] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/csv.rb')
-rw-r--r--lib/csv.rb10
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/csv.rb b/lib/csv.rb
index fd493d8859..e1e151b802 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -284,11 +284,15 @@ class CSV
#
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
#
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] }