Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1386,9 +1386,9 @@ def filter(input=nil, output=nil, **options)
# encoding: 'UTF-32BE:UTF-8'
# would read +UTF-32BE+ data from the file
# but transcode it to +UTF-8+ before parsing.
def foreach(path, mode="r", **options, &block)
return to_enum(__method__, path, mode, **options) unless block_given?
open(path, mode, **options) do |csv|
def foreach(path_or_io, mode="r", **options, &block)
return to_enum(__method__, path_or_io, mode, **options) unless block_given?
open(path_or_io, mode, **options) do |csv|
Comment on lines +1389 to +1391
csv.each(&block)
end
end
Expand Down Expand Up @@ -1582,7 +1582,7 @@ def generate_lines(rows, **options)
# * Arguments <tt>**options</tt> must be keyword options.
# See {Options for Generating}[#class-CSV-label-Options+for+Generating].
# * This method optionally accepts an additional <tt>:encoding</tt> option
# that you can use to specify the Encoding of the data read from +path+ or +io+.
# that you can use to specify the Encoding of the data read from +path_or_io+.
# You must provide this unless your data is in the encoding
# given by <tt>Encoding::default_external</tt>.
# Parsing will use this to determine how to parse the data.
Expand Down Expand Up @@ -1644,11 +1644,11 @@ def generate_lines(rows, **options)
# Raises an exception if the argument is not a \String object or \IO object:
# # Raises TypeError (no implicit conversion of Symbol into String)
# CSV.open(:foo)
def open(filename_or_io, mode="r", **options)
def open(path_or_io, mode="r", **options)
# wrap a File opened with the remaining +args+ with no newline
# decorator
file_opts = {}
may_enable_bom_detection_automatically(filename_or_io,
may_enable_bom_detection_automatically(path_or_io,
mode,
Comment on lines +1647 to 1652
options,
file_opts)
Expand All @@ -1661,11 +1661,11 @@ def open(filename_or_io, mode="r", **options)
options.delete(:replace)
options.delete_if {|k, _| /newline\z/.match?(k)}

if filename_or_io.is_a?(StringIO)
f = create_stringio(filename_or_io.string, mode, **file_opts)
if path_or_io.is_a?(StringIO)
f = create_stringio(path_or_io.string, mode, **file_opts)
else
begin
f = File.open(filename_or_io, mode, **file_opts)
f = File.open(path_or_io, mode, **file_opts)
rescue ArgumentError => e
raise unless /needs binmode/.match?(e.message) and mode == "r"
mode = "rb"
Expand Down Expand Up @@ -1919,16 +1919,16 @@ def parse_line(line, **options)
# path = 't.csv'
# File.write(path, string)
# CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
def read(path, **options)
open(path, **options) { |csv| csv.read }
def read(path_or_io, **options)
open(path_or_io, **options) { |csv| csv.read }
end

# :call-seq:
# CSV.readlines(source, **options)
#
# Alias for CSV.read.
def readlines(path, **options)
read(path, **options)
def readlines(path_or_io, **options)
read(path_or_io, **options)
Comment on lines +1922 to +1931
end

# :call-seq:
Expand All @@ -1946,25 +1946,25 @@ def readlines(path, **options)
# path = 't.csv'
# File.write(path, string)
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:4>
def table(path, **options)
def table(path_or_io, **options)
default_options = {
headers: true,
converters: :numeric,
header_converters: :symbol,
}
options = default_options.merge(options)
read(path, **options)
read(path_or_io, **options)
Comment on lines +1949 to +1956
end

ON_WINDOWS = /mingw|mswin/.match?(RUBY_PLATFORM)
private_constant :ON_WINDOWS

private
def may_enable_bom_detection_automatically(filename_or_io,
def may_enable_bom_detection_automatically(path_or_io,
mode,
options,
file_opts)
if filename_or_io.is_a?(StringIO)
if path_or_io.is_a?(StringIO)
# Support to StringIO was dropped for Ruby 2.6 and earlier without BOM support:
# https://github.com/ruby/stringio/pull/47
return if RUBY_VERSION < "2.7"
Expand Down
Loading