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

Ruby on-RailsRuby 1.9

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

That worked for me:

$ export LC_ALL=en_US.UTF-8
$ export LANG=en_US.UTF-8

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTamView Question on Stackoverflow
Solution 1 - Ruby on-RailsJarek ZmudzinskiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsShamuView Answer on Stackoverflow
Solution 3 - Ruby on-RailsIsmohView Answer on Stackoverflow
Solution 4 - Ruby on-RailsCassio CabralView Answer on Stackoverflow
Solution 5 - Ruby on-RailsPhil MillerView Answer on Stackoverflow
Solution 6 - Ruby on-RailsNowakerView Answer on Stackoverflow