Ruby 1.9 - invalid multibyte char (US-ASCII)

I am trying to run a rails (2.3.5) application on Ruby 1.9, I have this function that does some conversions in a string:

def replace_special_chars(downcase = true) if downcase string = self.downcase else string = self end string.gsub! /á|ã|à|ä|â/, 'a' string.gsub! /é|è|ë|ê/, 'e' string.gsub! /í|ì|ï|î/, 'i' string.gsub! /ó|õ|ò|ô|ö/, 'o' string.gsub! /ú|ù|ü|û/, 'u' string.gsub! /ç/, 'c' string.gsub! /&/, 'e' string.gsub! /\s/, '-' string.gsub! /[^a-zA-Z_0-9-]/, '' string.gsub! /-(-)+/, '-' string end 

But when I try to start the server, I got this error:

 <internal:lib/rubygems/custom_require>:29:in `require': /Users/.../lib/nzn_string.rb:11: invalid multibyte char (US-ASCII) (SyntaxError) /Users/.../lib/nzn_string.rb:11: invalid multibyte char (US-ASCII) /Users/.../lib/nzn_string.rb:11: syntax error, unexpected $end, expecting keyword_end string.gsub! /á|ã|à|ä|â/, 'a' ^ 

from: 29: to `require '

What is the right way to do this on ruby ​​1.9? I do not know what is missing here.

+45
ruby ruby-on-rails-3 ascii
Sep 09 '10 at 15:44
source share
3 answers

Write # encoding: utf-8 on top of this file. This changes the default encoding for all string / regexp literals in this utf-8 file. The default encoding for all US-ASCII literals, which cannot represent á .

+122
Sep 09 '10 at 15:47
source share
— -

To make it all over the project, try the magic_encoding gem.

+3
Nov 09 '11 at 6:18
source share

I think you can also change regular expressions from / re / syntax for syntax (Regexp.new 're', nil, 'n')

For example, the specified instruction:

string.gsub! / á | ã | à | ä | â /, 'a'

will become:

string.gsub! (Regexp.new 'á | ã | à | ä | â', nil, 'n'), 'a'

More details here:

http://www.ruby-forum.com/topic/183413

0
Apr 23 '13 at 16:20
source share



All Articles