File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -1581,7 +1581,14 @@ def generate_lines(rows, **options)
1581
1581
def open ( filename , mode = "r" , **options )
1582
1582
# wrap a File opened with the remaining +args+ with no newline
1583
1583
# decorator
1584
- file_opts = options . dup
1584
+ file_opts = { }
1585
+ have_encoding_options = ( options . key? ( :encoding ) or
1586
+ options . key? ( :external_encoding ) or
1587
+ mode . include? ( ":" ) )
1588
+ if not have_encoding_options and Encoding . default_external == Encoding ::UTF_8
1589
+ file_opts [ :encoding ] = "bom|utf-8"
1590
+ end
1591
+ file_opts . merge! ( options )
1585
1592
unless file_opts . key? ( :newline )
1586
1593
file_opts [ :universal_newline ] ||= false
1587
1594
end
Original file line number Diff line number Diff line change @@ -215,6 +215,16 @@ def test_open_with_newline
215
215
end
216
216
end
217
217
218
+ def test_open_with_bom
219
+ csv_data = @input . read
220
+ bom = "\ufeff " # U+FEFF ZERO WIDTH NO-BREAK SPACE
221
+ File . binwrite ( @input . path , "#{ bom } #{ csv_data } " )
222
+ @input . rewind
223
+ CSV . open ( @input . path , col_sep : "\t " ) do |csv |
224
+ assert_equal ( @rows , csv . to_a )
225
+ end
226
+ end
227
+
218
228
def test_parse
219
229
assert_equal ( @rows ,
220
230
CSV . parse ( @data , col_sep : "\t " , row_sep : "\r \n " ) )
You can’t perform that action at this time.
0 commit comments