Skip to content

Commit 1f9cbc1

Browse files
committed
Use "\n" for the default row separator on Ruby 3.0 or later
1 parent 744e411 commit 1f9cbc1

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

lib/csv.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@
9090
# with any questions.
9191

9292
require "forwardable"
93-
require "English"
9493
require "date"
9594
require "stringio"
9695

9796
require_relative "csv/fields_converter"
97+
require_relative "csv/input_record_separator"
9898
require_relative "csv/match_p"
9999
require_relative "csv/parser"
100100
require_relative "csv/row"
@@ -1187,7 +1187,7 @@ def instance(data = $stdout, **options)
11871187
# See {Options for Parsing}[#class-CSV-label-Options+for+Parsing].
11881188
def filter(input=nil, output=nil, **options)
11891189
# parse options for input, output, or both
1190-
in_options, out_options = Hash.new, {row_sep: $INPUT_RECORD_SEPARATOR}
1190+
in_options, out_options = Hash.new, {row_sep: InputRecordSeparator.value}
11911191
options.each do |key, value|
11921192
case key.to_s
11931193
when /\Ain(?:put)?_(.+)\Z/
@@ -1407,8 +1407,8 @@ def generate(str=nil, **options)
14071407
# Argument +ary+ must be an \Array.
14081408
#
14091409
# Special options:
1410-
# * Option <tt>:row_sep</tt> defaults to <tt>$INPUT_RECORD_SEPARATOR</tt>
1411-
# (<tt>$/</tt>).:
1410+
# * Option <tt>:row_sep</tt> defaults to <tt>"\n"> on Ruby 3.0 or later
1411+
# and <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>) otherwise.:
14121412
# $INPUT_RECORD_SEPARATOR # => "\n"
14131413
# * This method accepts an additional option, <tt>:encoding</tt>, which sets the base
14141414
# Encoding for the output. This method will try to guess your Encoding from
@@ -1430,7 +1430,7 @@ def generate(str=nil, **options)
14301430
# CSV.generate_line(:foo)
14311431
#
14321432
def generate_line(row, **options)
1433-
options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options)
1433+
options = {row_sep: InputRecordSeparator.value}.merge(options)
14341434
str = +""
14351435
if options[:encoding]
14361436
str.force_encoding(options[:encoding])

lib/csv/input_record_separator.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "English"
2+
require "stringio"
3+
4+
class CSV
5+
module InputRecordSeparator
6+
class << self
7+
is_input_record_separator_deprecated = false
8+
verbose, $VERBOSE = $VERBOSE, true
9+
stderr, $stderr = $stderr, StringIO.new
10+
input_record_separator = $INPUT_RECORD_SEPARATOR
11+
begin
12+
$INPUT_RECORD_SEPARATOR = "\r\n"
13+
is_input_record_separator_deprecated = (not $stderr.string.empty?)
14+
ensure
15+
$INPUT_RECORD_SEPARATOR = input_record_separator
16+
$stderr = stderr
17+
$VERBOSE = verbose
18+
end
19+
20+
if is_input_record_separator_deprecated
21+
def value
22+
"\n"
23+
end
24+
else
25+
def value
26+
$INPUT_RECORD_SEPARATOR
27+
end
28+
end
29+
end
30+
end
31+
end

lib/csv/parser.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "strscan"
44

55
require_relative "delete_suffix"
6+
require_relative "input_record_separator"
67
require_relative "match_p"
78
require_relative "row"
89
require_relative "table"
@@ -605,7 +606,7 @@ def resolve_row_separator(separator)
605606
# do nothing: ensure will set default
606607
end
607608
end
608-
separator = $INPUT_RECORD_SEPARATOR if separator == :auto
609+
separator = InputRecordSeparator.value if separator == :auto
609610
end
610611
separator.to_s.encode(@encoding)
611612
end

lib/csv/writer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative "input_record_separator"
34
require_relative "match_p"
45
require_relative "row"
56

@@ -133,7 +134,7 @@ def prepare_format
133134
@column_separator = @options[:column_separator].to_s.encode(@encoding)
134135
row_separator = @options[:row_separator]
135136
if row_separator == :auto
136-
@row_separator = $INPUT_RECORD_SEPARATOR.encode(@encoding)
137+
@row_separator = InputRecordSeparator.value.encode(@encoding)
137138
else
138139
@row_separator = row_separator.to_s.encode(@encoding)
139140
end

0 commit comments

Comments
 (0)