为ruby 1.9设置全局默认编码

时间:2021-12-13 15:59:13

I want to tell ruby that everything is utf8, except when stated otherwise, so I dont have to place these # encoding: utf-8 comments everywhere.

我想告诉ruby一切都是utf8,除非另有说明,所以我不必将这些#coding:utf-8评论放在各处。

3 个解决方案

#1


15  

You can either:

你可以:

  1. set your RUBYOPT environment variable to "-E utf-8"
  2. 将您的RUBYOPT环境变量设置为“-E utf-8”
  3. or use https://github.com/m-ryan/magic_encoding
  4. 或使用https://github.com/m-ryan/magic_encoding

#2


12  

If you're using environment variables, the general way is to use LC_ALL / LANG

如果您正在使用环境变量,一般方法是使用LC_ALL / LANG

Neither is set : fallback to US-ASCII

两者都没有设置:回退到US-ASCII

$ LC_ALL= LANG= ruby -e 'p Encoding.default_external'
#<Encoding:US-ASCII>

Either is set : that value is used

要么设置:使用该值

$ LC_ALL=en_US.UTF-8 LANG= ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

$ LC_ALL= LANG=en_US.UTF-8 ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

Both are set : LC_ALL takes precedence

两者都设置:LC_ALL优先

$ LC_ALL=C LANG=en_US.UTF-8 ruby -e 'p Encoding.default_external'
#<Encoding:US-ASCII>

$ LC_ALL=en_US.UTF-8 LANG=C ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

#3


0  

I just upgraded from 1.9 to 2.0, but for some reason the default external encoding was still set to ASCII. I was able to fix it by typing the following in Terminal:

我刚刚从1.9升级到2.0,但由于某种原因,默认外部编码仍然设置为ASCII。我可以通过在终端中键入以下内容来修复它:

export RUBYOPT='-E utf-8'

#1


15  

You can either:

你可以:

  1. set your RUBYOPT environment variable to "-E utf-8"
  2. 将您的RUBYOPT环境变量设置为“-E utf-8”
  3. or use https://github.com/m-ryan/magic_encoding
  4. 或使用https://github.com/m-ryan/magic_encoding

#2


12  

If you're using environment variables, the general way is to use LC_ALL / LANG

如果您正在使用环境变量,一般方法是使用LC_ALL / LANG

Neither is set : fallback to US-ASCII

两者都没有设置:回退到US-ASCII

$ LC_ALL= LANG= ruby -e 'p Encoding.default_external'
#<Encoding:US-ASCII>

Either is set : that value is used

要么设置:使用该值

$ LC_ALL=en_US.UTF-8 LANG= ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

$ LC_ALL= LANG=en_US.UTF-8 ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

Both are set : LC_ALL takes precedence

两者都设置:LC_ALL优先

$ LC_ALL=C LANG=en_US.UTF-8 ruby -e 'p Encoding.default_external'
#<Encoding:US-ASCII>

$ LC_ALL=en_US.UTF-8 LANG=C ruby -e 'p Encoding.default_external'
#<Encoding:UTF-8>

#3


0  

I just upgraded from 1.9 to 2.0, but for some reason the default external encoding was still set to ASCII. I was able to fix it by typing the following in Terminal:

我刚刚从1.9升级到2.0,但由于某种原因,默认外部编码仍然设置为ASCII。我可以通过在终端中键入以下内容来修复它:

export RUBYOPT='-E utf-8'