问题描述
我试图使我的rails应用程序(2.3.5)在Ruby 1.9上运行,我有这个函数可以对字符串进行一些转换:
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'
^
来自:'require'中的:29:
from :29:in `require'
在ruby 1.9上执行此操作的正确方法是什么?我不知道我在这里想念什么
What's the right way to do this on ruby 1.9?I don't know what im missing here
推荐答案
在该文件顶部写入# encoding: utf-8
.这将更改该文件utf-8
中所有字符串/正则表达式文字的默认编码.所有文字的默认编码为US-ASCII
,不能表示á
.
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 á
.
这篇关于Ruby 1.9-无效的多字节字符(US-ASCII)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!