使Ruby 1.9将所有源文件视为UTF-8编码。 (即使重新编译口译员是必要的) [英] Make Ruby 1.9 regard all source files to be UTF-8 encoded. (Even if recompiling the interpreter is necessary)

查看:176
本文介绍了使Ruby 1.9将所有源文件视为UTF-8编码。 (即使重新编译口译员是必要的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个rails应用程序从Ruby 1.8.7移植到1.9.2。一些文件在字符串和注释中都包含ä/ö/ü等变音符号。
这些文件保存为UTF-8,但没有 BOM (字节顺序标记)一开始



如您所知, Ruby 1.9拒绝解析这些文件,给出一个无效的多字节字符(US-ASCII)



我是谷歌和阅读很多但是唯一的解决方案似乎是




  • 插入物料清单或

  • insert每个开头的#编码:utf-8





我的编辑器(gEdit)似乎没有插入BOM。我也读到,有一个BOM是不好的做法,因为它可能会打破一些编辑器,它也会打破shell脚本,如果你想使用shebang符号。



em> EDIT :BOM打破了Ruby 1.8.7解析器,给出了一个语法错误,意外的kEND,期待文件的$ end(SyntaxError) / em>



我尝试强制使用ruby -Eutf-8:utf-8的外部编码,但这在调用rake时似乎被忽略(我试过:/ home / malte / .rvm / gems / ruby​​-1.9.2-p180 / bin / rake test)。



所以我的问题是:



由于RVM正在从源代码构建ruby 1.9,是否有一个构建选项或修补程序将默认编码从US-ASCII更改为UTF-8?



我快速浏览了源代码,但找不到默认设置的行(我不是C专家,很难)。

解决方案

我发现了一个解决方法:
设置 RUBYOPT 环境变量,例如执行



export RUBYOPT = -Ku



在你的shell中。



这将在调用ruby时设置-Ku als default选项。您现在可以调用所有其他调用ruby的工具,而不用担心参数。 rails server rake 将所有文件视为UTF-8。不需要BOM或魔术评论!


I want to port a rails app from Ruby 1.8.7 to 1.9.2. Some of the files contain umlauts like ä/ö/ü both within strings and comments. The files were saved as UTF-8 but without a BOM (byte order mark) at the beginning.

As you might know, Ruby 1.9 refuses to parse these files, giving an invalid multibyte char (US-ASCII)

I was googling and reading a lot but the only solution to this seems to be to

  • insert a BOM or
  • insert # coding: utf-8

at the beginning of each file.

My editor of choice (gEdit) doesn't seem to insert a BOM. I also read that having a BOM is bad practice because it may break some editors, it also breaks shell scripts if you want to use the shebang notation.

EDIT: The BOM breaks the Ruby 1.8.7 parser, giving a syntax error, unexpected kEND, expecting $end (SyntaxError) for the file!

I tried forcing the external encoding with ruby -Eutf-8:utf-8 but this seems to be ignored when calling rake (I tried: /home/malte/.rvm/gems/ruby-1.9.2-p180/bin/rake test).

So my question is:

As RVM is building ruby 1.9 from source anyway, is there a build option or a patch to change the default encoding from US-ASCII to UTF-8?

I took a quick look at the source code but couldn't find the line where the default is set (I'm no C expert, tough).

解决方案

I found a workaround: set the RUBYOPT environment variable, for example by executing

export RUBYOPT=-Ku

in your shell.

This will set -Ku als default option when calling ruby. You can now call all other tools which invoke ruby without worrying about parameters. rails server or rake works and regards all files as UTF-8. No BOM or magic comments necessary!

这篇关于使Ruby 1.9将所有源文件视为UTF-8编码。 (即使重新编译口译员是必要的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆