Set UTF-8 as default for Ruby 1.9.3

前端 未结 4 1036
孤独总比滥情好
孤独总比滥情好 2020-12-05 20:01

I\'m on Rails 4 and Ruby 1.9.3

I use \"strange\" characters very often, so I have to declare UTF-8 encoding at the top of all .rb files.

Is there any way to

相关标签:
4条回答
  • 2020-12-05 20:25

    in Ruby 1.9 the default is ASCII

    in Ruby 2.0 the default is UTF-8.


    change Ruby version

    or

    config.encoding = "utf-8" # application.rb
    

    and in your database.yml

    development:
         adapter:  your_db
         host:     localhost
         encoding: utf8
    
    0 讨论(0)
  • 2020-12-05 20:26

    I think you would want one of the following, depending on the context.

    Encoding.default_internal = Encoding::UTF_8
    Encoding.default_external = Encoding::UTF_8
    

    This setting is made in the environment.rb file.

    0 讨论(0)
  • 2020-12-05 20:30

    To change the source encoding (i.e. the encoding your actual written source code is in), you have to use the magic comment currently:

    # encoding: utf-8
    

    It is not enough to either set the internal encoding (the encoding of the internal string representation after conversion) or the external encoding (the assumed encoding of read files). You actually have to set the magic encoding comment on top of files to set the source encoding.

    In ChiliProject we have a rake task which sets the correct encoding header in all files automatically before a release.

    As for encoding defaults:

    • Ruby 1.8 and below didn't knew the concept of string encodings at all. Strings were more or less byte arrays.
    • Ruby 1.9: default string encoding is US_ASCII everywhere.
    • Ruby 2.0 and above: default string encoding is UTF-8.

    Thus, if you use Ruby 2.0, you could skip the encoding comment and correctly assume UTF-8 encoding everywhere by default.

    0 讨论(0)
  • 2020-12-05 20:35

    In your application.rb

    # Configure the default encoding used in templates for Ruby
    config.encoding = "utf-8"
    

    This is not the whole story as pointed out by Holger, check out this question for further explanation.

    0 讨论(0)
提交回复
热议问题