Skip to content

Commit 1f39fd2

Browse files
committed
Move CSV row encoding calculation to its own method
1 parent 382ab50 commit 1f39fd2

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

lib/csv.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,17 +1506,7 @@ def generate_line(row, **options)
15061506
if options[:encoding]
15071507
str.force_encoding(options[:encoding])
15081508
else
1509-
fallback_encoding = nil
1510-
output_encoding = nil
1511-
row.each do |field|
1512-
next unless field.is_a?(String)
1513-
fallback_encoding ||= field.encoding
1514-
next if field.ascii_only?
1515-
output_encoding = field.encoding
1516-
break
1517-
end
1518-
output_encoding ||= fallback_encoding
1519-
if output_encoding
1509+
if output_encoding = row_encoding(row)
15201510
str.force_encoding(output_encoding)
15211511
end
15221512
end
@@ -1960,6 +1950,20 @@ def table(path, **options)
19601950
private_constant :ON_WINDOWS
19611951

19621952
private
1953+
1954+
def row_encoding(row)
1955+
fallback_encoding = nil
1956+
output_encoding = nil
1957+
row.each do |field|
1958+
next unless field.is_a?(String)
1959+
fallback_encoding ||= field.encoding
1960+
next if field.ascii_only?
1961+
output_encoding = field.encoding
1962+
break
1963+
end
1964+
output_encoding || fallback_encoding
1965+
end
1966+
19631967
def may_enable_bom_detection_automatically(filename_or_io,
19641968
mode,
19651969
options,

0 commit comments

Comments
 (0)