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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Tam
  • 11,872
  • 19
  • 69
  • 119
  • 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. – boulder_ruby Aug 13 '12 at 04:06

6 Answers6

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Jarek Zmudzinski
  • 7,121
  • 1
  • 15
  • 7
  • 2
    Hmm.........added that to the top of the file but still get the same error message. Any suggestions? – Artem Kalinchuk Mar 19 '12 at 15:33
  • @ArtemKalinchuk Just add it directly under `#!/bin/env ruby` I had a blank line between them and it didn't work. – Thomas Jun 28 '12 at 12:56
  • 8
    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 :-) – bass-t Aug 13 '12 at 09:58
  • Awesome! How to set this for all files of my Rails project? – sparkle Oct 27 '12 at 21:47
  • 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. – Marcel Valdez Orozco Oct 29 '12 at 22:05
  • 2
    `#!/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. – gak Apr 07 '13 at 05:32
  • make sure after setting that magic comment that your file is encoded in utf-8 – Postscripter Jun 23 '13 at 12:23
  • 10
    `# 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. – thomax Sep 16 '13 at 10:09
  • 1
    @gotqn - Please find the same article here - http://graysoftinc.com/character-encodings/ruby-19s-three-default-encodings – Alok Swain Apr 01 '15 at 07:18
  • This worked for me on Ubuntu 14.04, running rails 3.2.11 – Cyclonecode Oct 06 '15 at 09:31
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.

epologee
  • 11,229
  • 11
  • 68
  • 104
Shamu
  • 499
  • 4
  • 3
  • 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. – Marcel Valdez Orozco Oct 29 '12 at 22:06
  • adding 'gem magic_encoding' to gemfile on rails 2.3 & ruby 1.9 helped – Elmor Dec 04 '12 at 18:11
  • this doesn't integrate into cucumber tests. – Trip Aug 12 '13 at 17:34
  • 1
    @Elmor 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. – Nowaker Mar 06 '14 at 19:24
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.

Ismoh
  • 1,074
  • 2
  • 12
  • 35
12

That worked for me:

$ export LC_ALL=en_US.UTF-8
$ export LANG=en_US.UTF-8
Cassio Cabral
  • 2,652
  • 3
  • 23
  • 37
8

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

Phil Miller
  • 36,389
  • 13
  • 67
  • 90
8

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

Nowaker
  • 12,154
  • 4
  • 56
  • 62