Why is the default encoding in Rails not UTF-8?

痞子三分冷 提交于 2019-12-22 11:46:10

问题


When I use UTF-8 characters (most of the time Turkish characters) in controllers, I have to add #encoding: utf-8 on the top of each controller file. Why doesn't Rails use this as a default?

edit: I have learnt that it is not about Rails, it is about Ruby.


回答1:


Ruby 2.0 is UTF8 by default. Otherwise you must signify that in 1.9. According to naruse:

The default script encoding change.

Default script encoding (when magic comment is not specified) is changed into UTF8[#6679] In Ruby 1.9, the default script encoding is US-ASCII. We changed it to be UTF-8 after considering the following pros and cons. UTF-8 default is convenient because the majority of modern application uses UTF-8 This change doe not impact any 1.9 codes if Magic Comments are in place. The default script encoding in 1.9 without Magic Comment is either US-ASCII or ASCII-8BIT. In UTF-8, then some string manipulation could become slower.

Source: Rubyist Magazine




回答2:


Rails is not a programming language. It is just library written in Ruby. It cannot change the fundamental syntax of Ruby. Ruby 1.8 doesn't know anything about encodings, in Ruby 1.9 the default is ASCII and in Ruby 2.0 the default is UTF-8. There's nothing Rails can do about it.



来源:https://stackoverflow.com/questions/16561365/why-is-the-default-encoding-in-rails-not-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!