diff --git a/lib/csv.rb b/lib/csv.rb
index 3881ba24c2..91aeb19a3c 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -90,11 +90,11 @@
# with any questions.
require "forwardable"
-require "English"
require "date"
require "stringio"
require_relative "csv/fields_converter"
+require_relative "csv/input_record_separator"
require_relative "csv/match_p"
require_relative "csv/parser"
require_relative "csv/row"
@@ -1051,7 +1051,7 @@ class CSV
# out_string # => "FOO,0000\nBAR,1111\nBAZ,2222\n"
def filter(input=nil, output=nil, **options)
# parse options for input, output, or both
- in_options, out_options = Hash.new, {row_sep: $INPUT_RECORD_SEPARATOR}
+ in_options, out_options = Hash.new, {row_sep: InputRecordSeparator.value}
options.each do |key, value|
case key.to_s
when /\Ain(?:put)?_(.+)\Z/
@@ -1292,8 +1292,8 @@ class CSV
# Argument +ary+ must be an \Array.
#
# Special options:
- # * Option :row_sep defaults to $INPUT_RECORD_SEPARATOR
- # ($/).:
+ # * Option :row_sep defaults to "\n"> on Ruby 3.0 or later
+ # and $INPUT_RECORD_SEPARATOR ($/) otherwise.:
# $INPUT_RECORD_SEPARATOR # => "\n"
# * This method accepts an additional option, :encoding, which sets the base
# Encoding for the output. This method will try to guess your Encoding from
@@ -1315,7 +1315,7 @@ class CSV
# CSV.generate_line(:foo)
#
def generate_line(row, **options)
- options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options)
+ options = {row_sep: InputRecordSeparator.value}.merge(options)
str = +""
if options[:encoding]
str.force_encoding(options[:encoding])
diff --git a/lib/csv/input_record_separator.rb b/lib/csv/input_record_separator.rb
new file mode 100644
index 0000000000..bbf13479f7
--- /dev/null
+++ b/lib/csv/input_record_separator.rb
@@ -0,0 +1,31 @@
+require "English"
+require "stringio"
+
+class CSV
+ module InputRecordSeparator
+ class << self
+ is_input_record_separator_deprecated = false
+ verbose, $VERBOSE = $VERBOSE, true
+ stderr, $stderr = $stderr, StringIO.new
+ input_record_separator = $INPUT_RECORD_SEPARATOR
+ begin
+ $INPUT_RECORD_SEPARATOR = "\r\n"
+ is_input_record_separator_deprecated = (not $stderr.string.empty?)
+ ensure
+ $INPUT_RECORD_SEPARATOR = input_record_separator
+ $stderr = stderr
+ $VERBOSE = verbose
+ end
+
+ if is_input_record_separator_deprecated
+ def value
+ "\n"
+ end
+ else
+ def value
+ $INPUT_RECORD_SEPARATOR
+ end
+ end
+ end
+ end
+end
diff --git a/lib/csv/parser.rb b/lib/csv/parser.rb
index d0b02a6423..0d8a157fd7 100644
--- a/lib/csv/parser.rb
+++ b/lib/csv/parser.rb
@@ -3,6 +3,7 @@
require "strscan"
require_relative "delete_suffix"
+require_relative "input_record_separator"
require_relative "match_p"
require_relative "row"
require_relative "table"
@@ -605,7 +606,7 @@ class CSV
# do nothing: ensure will set default
end
end
- separator = $INPUT_RECORD_SEPARATOR if separator == :auto
+ separator = InputRecordSeparator.value if separator == :auto
end
separator.to_s.encode(@encoding)
end
diff --git a/lib/csv/writer.rb b/lib/csv/writer.rb
index d49115fccf..4a9a35c5af 100644
--- a/lib/csv/writer.rb
+++ b/lib/csv/writer.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: true
+require_relative "input_record_separator"
require_relative "match_p"
require_relative "row"
@@ -133,7 +134,7 @@ class CSV
@column_separator = @options[:column_separator].to_s.encode(@encoding)
row_separator = @options[:row_separator]
if row_separator == :auto
- @row_separator = $INPUT_RECORD_SEPARATOR.encode(@encoding)
+ @row_separator = InputRecordSeparator.value.encode(@encoding)
else
@row_separator = row_separator.to_s.encode(@encoding)
end