Skip to content

Commit 19c7649

Browse files
authored
Unify first parameter name to path_or_io across methods (#358)
## Overview Previously, the first parameter name was inconsistent across methods and their RDoc comments, with names such as `path`, `source`, and `filename_or_io` used interchangeably despite referring to the same concept: a file path or an IO object. This change aligns all class methods to use `path_or_io`. This more accurately conveys that the argument accepts either a file path or an IO object. ## Reference - #302
1 parent 79eb55a commit 19c7649

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

lib/csv.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ def filter(input=nil, output=nil, **options)
13771377
# * Arguments <tt>**options</tt> must be keyword options.
13781378
# See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
13791379
# * This method optionally accepts an additional <tt>:encoding</tt> option
1380-
# that you can use to specify the Encoding of the data read from +path+ or +io+.
1380+
# that you can use to specify the Encoding of the data read from +path_or_io+.
13811381
# You must provide this unless your data is in the encoding
13821382
# given by <tt>Encoding::default_external</tt>.
13831383
# Parsing will use this to determine how to parse the data.
@@ -1386,9 +1386,9 @@ def filter(input=nil, output=nil, **options)
13861386
# encoding: 'UTF-32BE:UTF-8'
13871387
# would read +UTF-32BE+ data from the file
13881388
# but transcode it to +UTF-8+ before parsing.
1389-
def foreach(path, mode="r", **options, &block)
1390-
return to_enum(__method__, path, mode, **options) unless block_given?
1391-
open(path, mode, **options) do |csv|
1389+
def foreach(path_or_io, mode="r", **options, &block)
1390+
return to_enum(__method__, path_or_io, mode, **options) unless block_given?
1391+
open(path_or_io, mode, **options) do |csv|
13921392
csv.each(&block)
13931393
end
13941394
end
@@ -1582,7 +1582,7 @@ def generate_lines(rows, **options)
15821582
# * Arguments <tt>**options</tt> must be keyword options.
15831583
# See {Options for Generating}[#class-CSV-label-Options+for+Generating].
15841584
# * This method optionally accepts an additional <tt>:encoding</tt> option
1585-
# that you can use to specify the Encoding of the data read from +path+ or +io+.
1585+
# that you can use to specify the Encoding of the data read from +path_or_io+.
15861586
# You must provide this unless your data is in the encoding
15871587
# given by <tt>Encoding::default_external</tt>.
15881588
# Parsing will use this to determine how to parse the data.
@@ -1644,11 +1644,11 @@ def generate_lines(rows, **options)
16441644
# Raises an exception if the argument is not a \String object or \IO object:
16451645
# # Raises TypeError (no implicit conversion of Symbol into String)
16461646
# CSV.open(:foo)
1647-
def open(filename_or_io, mode="r", **options)
1647+
def open(path_or_io, mode="r", **options)
16481648
# wrap a File opened with the remaining +args+ with no newline
16491649
# decorator
16501650
file_opts = {}
1651-
may_enable_bom_detection_automatically(filename_or_io,
1651+
may_enable_bom_detection_automatically(path_or_io,
16521652
mode,
16531653
options,
16541654
file_opts)
@@ -1661,11 +1661,11 @@ def open(filename_or_io, mode="r", **options)
16611661
options.delete(:replace)
16621662
options.delete_if {|k, _| /newline\z/.match?(k)}
16631663

1664-
if filename_or_io.is_a?(StringIO)
1665-
f = create_stringio(filename_or_io.string, mode, **file_opts)
1664+
if path_or_io.is_a?(StringIO)
1665+
f = create_stringio(path_or_io.string, mode, **file_opts)
16661666
else
16671667
begin
1668-
f = File.open(filename_or_io, mode, **file_opts)
1668+
f = File.open(path_or_io, mode, **file_opts)
16691669
rescue ArgumentError => e
16701670
raise unless /needs binmode/.match?(e.message) and mode == "r"
16711671
mode = "rb"
@@ -1901,10 +1901,10 @@ def parse_line(line, **options)
19011901

19021902
#
19031903
# :call-seq:
1904-
# read(source, **options) -> array_of_arrays
1905-
# read(source, headers: true, **options) -> csv_table
1904+
# read(path_or_io, **options) -> array_of_arrays
1905+
# read(path_or_io, headers: true, **options) -> csv_table
19061906
#
1907-
# Opens the given +source+ with the given +options+ (see CSV.open),
1907+
# Opens the given +path_or_io+ with the given +options+ (see CSV.open),
19081908
# reads the source (see CSV#read), and returns the result,
19091909
# which will be either an \Array of Arrays or a CSV::Table.
19101910
#
@@ -1919,22 +1919,22 @@ def parse_line(line, **options)
19191919
# path = 't.csv'
19201920
# File.write(path, string)
19211921
# CSV.read(path, headers: true) # => #<CSV::Table mode:col_or_row row_count:4>
1922-
def read(path, **options)
1923-
open(path, **options) { |csv| csv.read }
1922+
def read(path_or_io, **options)
1923+
open(path_or_io, **options) { |csv| csv.read }
19241924
end
19251925

19261926
# :call-seq:
1927-
# CSV.readlines(source, **options)
1927+
# CSV.readlines(path_or_io, **options)
19281928
#
19291929
# Alias for CSV.read.
1930-
def readlines(path, **options)
1931-
read(path, **options)
1930+
def readlines(path_or_io, **options)
1931+
read(path_or_io, **options)
19321932
end
19331933

19341934
# :call-seq:
1935-
# CSV.table(source, **options)
1935+
# CSV.table(path_or_io, **options)
19361936
#
1937-
# Calls CSV.read with +source+, +options+, and certain default options:
1937+
# Calls CSV.read with +path_or_io+, +options+, and certain default options:
19381938
# - +headers+: +true+
19391939
# - +converters+: +:numeric+
19401940
# - +header_converters+: +:symbol+
@@ -1946,25 +1946,25 @@ def readlines(path, **options)
19461946
# path = 't.csv'
19471947
# File.write(path, string)
19481948
# CSV.table(path) # => #<CSV::Table mode:col_or_row row_count:4>
1949-
def table(path, **options)
1949+
def table(path_or_io, **options)
19501950
default_options = {
19511951
headers: true,
19521952
converters: :numeric,
19531953
header_converters: :symbol,
19541954
}
19551955
options = default_options.merge(options)
1956-
read(path, **options)
1956+
read(path_or_io, **options)
19571957
end
19581958

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

19621962
private
1963-
def may_enable_bom_detection_automatically(filename_or_io,
1963+
def may_enable_bom_detection_automatically(path_or_io,
19641964
mode,
19651965
options,
19661966
file_opts)
1967-
if filename_or_io.is_a?(StringIO)
1967+
if path_or_io.is_a?(StringIO)
19681968
# Support to StringIO was dropped for Ruby 2.6 and earlier without BOM support:
19691969
# https://github.com/ruby/stringio/pull/47
19701970
return if RUBY_VERSION < "2.7"

0 commit comments

Comments
 (0)