3

My script fails on this bad encoding, even I brought all files to UTF-8 but still some won't convert or just have wrong chars inside.

It fails actually on var assignment step.

Can I set some kind of error handling for this case like below so my loop will continue. That ¿ causes all problem.

Need to run this script all the way without errors. Tried already encoding und force_encoding and shebang line. Is Ruby has any kind of error handling routing so I can handle that bad case and continue with the rest of script? How to get rid of this error invalid multibyte char (UTF-8)

line = '¿USE [Alpha]' lineOK = ' USE [Alpha] OK line' >ruby ReadFile_Test.rb ReadFile_Test.rb:15: invalid multibyte char (UTF-8) 
8
  • Please read ruby-doc.org/core-2.1.2/Encoding.html Commented Sep 6, 2019 at 22:24
  • Running your code does not show any error to me. Also, it is not clear to me what you want to archieve ... Commented Sep 6, 2019 at 22:59
  • Have you tried to force encoding to UTF-8 e.g. line.force_encoding(Encoding::UTF_8) Commented Sep 7, 2019 at 1:20
  • Another option might be to convert to binary, then to ascii replacing invalid chars with something meaningful or with a space even `force_encoding('BINARY').encode('ASCII', :invalid => :replace, :undef => :replace, :replace => ' ') Commented Sep 7, 2019 at 1:29
  • @Christian, how you manage run it OK) on what version, what the trick? I am on Windows. Anybody else getting an error like me ? Tx Commented Sep 9, 2019 at 14:31

1 Answer 1

1

I could reproduce your issue by saving the file with ISO-8859-1 encoding.

Running your code with the file in this non UTF8-encoding the error popped up. My solution was to save the file as UTF-8.

I am using Sublime as text editor and there is the option 'file > save with encoding'. I have chosen 'UTF-8' and was able to run the script.

Using puts line.encoding showed me UTF-8 then and no error anymore.

I suggest to re-check the encoding of your saved script file again.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Christian, yes once I save my test file in UTF-8 it worked OK. I used ScITE, though it was not original problem, as this string came after read file, which has that char. I will check now how it work with File.read in script saved as UTF. Tx Much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.