views:

4448

answers:

4

Hi,

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?

Thanks,

Tam

+1  A: 

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

Novelocrat
Why is it misleading? They *are* multibyte characters.
Matthew Scharley
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.
Novelocrat
+4  A: 

Those particular quotation marks are not valid ASCII characters. Here is a reference for the different types of UTF quotation marks and their relationship to ASCII characters. If you need to accept those quotation marks you will need to accept UTF strings, which should be the default in Rails 2.3.x (IIRC). If you need to deal with handling UTF input in Rails I would refer to this article.

James Thompson
+13  A: 

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

# coding: utf-8

It worked for me like charm.

Jarek Zmudzinski
That is an easy fix for me. Later I can dig into why it works :)
Jesper Rønn-Jensen
+2  A: 

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" from the root of your app.

Shamu