invalid multibyte char (US-ASCII) with Rails and Ruby 1.9
Asked Answered
G

6

200

I'm using Ruby 1.9.1 with Rails 2.3.4 My application is to handle text input

If I try something like (the inside quotation marks look different)

text = "”“"

I get the following error:

#<SyntaxError: /Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: invalid multibyte char (US-ASCII)
/Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: invalid multibyte char (US-ASCII)
/Users/tammam56/rubydev/favquote/lib/daemons/twitter_quotes_fetch.rb:54: syntax error, unexpected $end, expecting keyword_end

I need to user those quotation marks as users might input them and I have to account for that?

Any ideas?

Gnaw answered 16/11, 2009 at 3:0 Comment(1)
If your code does not have any backticks in it but you're being "accused" of using backticks, there may be some weird spacing/tabs/newline issues in your file. Try posting it into a StackOverflow blank for example, and SO will start acting weird. Remove the strange spaces and tabs and newlines. Again, just pasting the code into a SO blank and trying to format your code for presentation is one way to give yourself a hint.Sanatory
L
690

Have you tried adding a magic comment in the script where you use non-ASCII chars? It should go on top of the script.

#!/bin/env ruby
# encoding: utf-8

It worked for me like a charm.

Ladawnladd answered 20/1, 2010 at 21:41 Comment(10)
Hmm.........added that to the top of the file but still get the same error message. Any suggestions?Bowshot
@ArtemKalinchuk Just add it directly under #!/bin/env ruby I had a blank line between them and it didn't work.Zitella
The central explanation can be found in the article @dalyons linked: source files receive a US-ASCII Encoding, unless you say otherwise. If you place any non-ASCII content in a String literal without changing the source Encoding, Ruby will die with that error. Thanks guys, I finally got it :-)Telstar
Awesome! How to set this for all files of my Rails project?Bianca
I'm sure you can come up with a series of pipelined bash instructions for adding this to all .rb, .erb, .yml, etc.. files in a RoR folder structure.Urinalysis
#!/bin/env ruby isn't necessary unless you're running the script from the command line as an executable. The # encoding line works by itself.Edrick
make sure after setting that magic comment that your file is encoded in utf-8Catawba
# encoding: utf-8. It's late 2013 and we still have to play this game. Hold on, phone ringing... It was 2033, they called to say they still play it. Oh well, thanks for reminding me, Jarek Zmudzinski from 2010.Lannielanning
@gotqn - Please find the same article here - graysoftinc.com/character-encodings/…Annoy
This worked for me on Ubuntu 14.04, running rails 3.2.11Melville
K
42

If you want to add magic comments on all the source files of a project easily, you can use the magic_encoding gem

sudo gem install magic_encoding

then just call magic_encoding in the terminal from the root of your app.

Kinsey answered 3/8, 2010 at 12:38 Comment(4)
I think it's important to remember this kind of details, so I wouldn't use that gem for at least a few months of writing # encoding: utf-8 manually.Urinalysis
adding 'gem magic_encoding' to gemfile on rails 2.3 & ruby 1.9 helpedOlomouc
this doesn't integrate into cucumber tests.Rizas
@Olomouc You should never put external libraries to your project's Gemfile like that. magic_encoding is just a command-line tool, not a project dependency.Irrelevancy
M
18

I just want to add my solution:

I use german umlauts like ö, ü, ä and got the same error.
@Jarek Zmudzinski just told you how it works, but here is mine:

Add this code to the top of your Controller: # encoding: UTF-8
(for example to use flash message with umlauts)

example of my Controller:

# encoding: UTF-8
class UserController < ApplicationController

Now you can use ö, ä ,ü, ß, "", etc.

Misbelief answered 10/4, 2014 at 13:3 Comment(0)
Q
12

That worked for me:

$ export LC_ALL=en_US.UTF-8
$ export LANG=en_US.UTF-8
Quiver answered 17/3, 2014 at 13:15 Comment(0)
S
8

Those slanted double quotes are not ASCII characters. The error message is misleading about them being 'multi-byte'.

Sublingual answered 16/11, 2009 at 3:6 Comment(2)
Why is it misleading? They are multibyte characters.Phane
Because ASCII doesn't define any multi-byte encodings. As for as ASCII is concerned, those are gibberish, that happens to be valid in a related encoding.Sublingual
I
8

Just a note that as of Ruby 2.0 there is no need to add # encoding: utf-8. UTF-8 is automatically detected.

Irrelevancy answered 7/1, 2014 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.