46

Im trying to make my rails application (2.3.5) to run on Ruby 1.9, I've this function that make some transformations on 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:in `require'

What's the right way to do this on ruby 1.9? I don't know what im missing here

Tiago
  • 2,966
  • 4
  • 33
  • 41
  • possible duplicate of [invalid multibyte char (US-ASCII) with Ruby on Rails](http://stackoverflow.com/questions/1739836/invalid-multibyte-char-us-ascii-with-ruby-on-rails) – user229044 Nov 15 '11 at 16:13
  • This link is helpful and it works for me [Stackoverflow answer] http://stackoverflow.com/questions/3916931/rails-3-invalid-multibyte-char-us-ascii?s=bed697c0-32eb-47d0-a22a-3b85ee418b0f#new-answer – manish nautiyal Jul 20 '13 at 05:39

3 Answers3

122

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

Reactormonk
  • 21,472
  • 14
  • 74
  • 123
3

To make it project-wide, try: the magic_encoding gem.

ohho
  • 50,879
  • 75
  • 256
  • 383
0

I think you can also change the regexps from the syntax /re/ to the syntax (Regexp.new 're', nil, 'n')

For example, the instruction you mentioned:

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

will become:

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

More details here:

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

Carlos Saltos
  • 1,385
  • 15
  • 15