1

I'm really confused on this one, and maybe it's a bug in Ruby 2.6.2. I have files that were written as UTF-8 with BOM, so I'm using the following:

filelist = Dir.entries(@input_dirname).join(' ')
filelist = filelist.split(' ').grep(/xml/)

filelist.each do |indfile|
  filecontents_tmp = File.read("#{@input_dirname}/#{indfile}", :encoding =>'bom|utf-8')
  puts filecontents_tmp
end

If I put a debug breakpoint at the puts line, my file is read in properly. If I just run the simple script, I get the following error:

in `read': ASCII incompatible encoding needs binmode (ArgumentError)

I'm confused as to why this would work in debug, but not when run normally. Ideas?

Wayne Brissette
  • 135
  • 2
  • 8

1 Answers1

0

Have you tried printing the default encoding when you run the file as opposed to when you debug the file? There are 3 ways to set / change the encoding in Ruby (that I'm aware of), so I wonder if it's different between running the file and debugging. You should be able to tell by printing the default encoding: puts Encoding.default_external.

As for actually fixing the issue, I ran into a similar problem and found this answer which said to add bin mode as an option to the File.open call and it worked for me.

Don
  • 506
  • 2
  • 8
  • 23