Invalid multibyte char with Ruby 1.9, setting encoding: utf-8 does not fix it
Use a tool such as od (assuming you're on Linux) to look at the actual contents of the file. If you have single byte C5, your file isn't in UTF-8. It's probably stored in ISO-8859-1 or similar, and Ruby is quite correctly complaining.
Ruby error invalid multibyte char (US-ASCII)
When you run the script with Ruby 1.9, change the first two lines of the script to:
#!/usr/bin/env ruby
# encoding: utf-8
require 'net/http'
This tells Ruby to run the script with support for the UTF-8
character set. Without that line Ruby 1.9 would default to the US_ASCII
character set.
Just for the record: This will not work in Ruby 1.8, because 1.8 doesn't knew anything about string encodings. And the line is not needed anymore in Ruby 2.0, because Ruby 2.0 is using UTF-8
as the default anyway.
Ruby 1.9 - invalid multibyte char (US-ASCII)
Write # encoding: utf-8
on top of that file. That changes the default encoding of all string/regexp literals in that file utf-8
. The default encoding for all literals is US-ASCII
, which cannot represent á
.
Rails 3 invalid multibyte char (US-ASCII)
Instead of adding # coding: UTF-8
try to add # encoding: UTF-8
on the first line of the file.
It worked for me. I found the information here : http://groups.google.com/group/sinatrarb/browse_thread/thread/f92529bf0cf62015
VSCode complains a Ruby UTF-8 file has invalid multibyte char (US-ASCII)
I think the issue is in the linter.
"ruby.lint": {
"reek": true,
"rubocop": true,
"ruby": {
"unicode": true,
},
"fasterer": true,
"debride": false,
"ruby-lint": false
},
in settings.json unicode is not turned on by default for ruby.lint so you need to do that manually.
invalid multibyte char (US-ASCII) in a chef recipe
The line in the code :
initdb –D –-no-locale –-encoding=UTF8
Should be:
initdb –D --no-locale --encoding=UTF8
Ruby invalid multibyte char error (Sep 2019)
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.
Invalid multibyte char (US-ASCII) error for ä, ü, ö, ß which are Ascii!
Put the magic comment # coding: utf-8
at the beginning your your script (on the second line if you're using shebang).
#!/usr/local/bin/ruby
# coding: utf-8
puts "i like my chars: ä, ü, ö and ß!"
Related Topics
Net::Ssh Sudo Command Hangs After Entering Password
Regex, How to Match Multiple Lines
How Is Each_With_Object Supposed to Work
Ruby - Digest::Digest Is Deprecated; Use Digest
Rails 3 Install Error: "Invalid Value for @Cert_Chain"
How to Create a Hash in Ruby That Compares Strings, Ignoring Case
To_Specs': Could Not Find Chef (>= 0) Amongst [] (Gem::Loaderror)
Calling Sinatra from Within Sinatra
Ruby Minitest: Suite- or Class- Level Setup
Detect If Application Was Started as Http Server or Not (Rake Task, Rconsole etc)
How to Get Ruby to Print a Full Backtrace Instead of a Truncated One
How to Round a Time Down to the Nearest 15 Minutes in Ruby
How to Implement Cookie Support in Ruby Net/Http
How to Remove the String "\N" from Within a Ruby String
Directly Accessing an Instance Variable VS. Using an Accessor Method