How can I avoid putting the magic encoding comment on top of every UTF-8 file in Ruby 1.9?

后端 未结 7 2109
野的像风
野的像风 2020-12-13 18:30

I have a Rails project with a lot of Cyrillic strings in it.

It worked fine on Ruby 1.8, but Ruby 1.9 assumes source files are US-ASCII-encoded unless you provide a

7条回答
  •  借酒劲吻你
    2020-12-13 19:09

    In my opinion, explicit is not always better than implicit.

    When almost all the source you use is UTF-8 compatible, you can easily avoid putting the magic encoding comment by using Ruby's -Ku command-line options.

    Don't confuse the "u" parameter of the -K options with -U options.

    -Ku : set internal and script encoding to utf-8
    -U  : set internal encoding to utf-8
    

    Then, set the magic encoding comment only in scripts that need it. Remember, convention over configuration!

    You can set the environment variable RUBYOPT=-Ku

    See Ruby's command-line options at http://www.manpagez.com/man/1/ruby/.

提交回复
热议问题