In Ruby on Rails, Are '#Encoding: Utf-8' and 'Config.Encoding = "Utf-8"' Different

In Ruby on Rails, are '#encoding: utf-8' and 'config.encoding = utf-8 ' different?

The config.encoding = "utf-8" part in config/application.rb is related to how rails should interpret content.

#encoding: utf-8 in a ruby file tells ruby that this file contains non-ascii characters.

These two cases are different. The first one (in config/application.rb) tells rails something, and has nothing at all to do with how ruby itself should interpret source files.

You can set the environment variable RUBYOPT=-Ku if you're lazy and want ruby to automatically set the default file encoding of .rb files to utf-8, but I'd rather recommend that you put your non-ascii bits in a translation file and reference that with I18n.t.

Set UTF-8 as default for Ruby 1.9.3

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.

Is UTF-8 the default encoding in Ruby v.2?

Yes. The fact that UTF-8 is the default encoding is only since Ruby 2.

If you are aware that his examples were from Ruby 1.9, then check the newly added features to the newer versions of Ruby. It is not that much.

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

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

Force strings to UTF-8 from any encoding

Ruby 1.9

"Forcing" an encoding is easy, however it won't convert the characters just change the encoding:

str = str.force_encoding('UTF-8')

str.encoding.name # => 'UTF-8'

If you want to perform a conversion, use encode:

begin
str.encode("UTF-8")
rescue Encoding::UndefinedConversionError
# ...
end

I would definitely read the following post for more information:

http://graysoftinc.com/character-encodings/ruby-19s-string

Rails and Utf-8 encoding

I couldn't fix this problem. I "solved" it by purchasing a Mac and continue to develop my Rails apps on it instead...

Encoding - me: 1-0

Rails utf-8 problem

See Joel spolsky's article "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)".

To quote the part that answers this questions concisely

The Single Most Important Fact About Encodings


If you completely forget everything I just explained, please remember
one extremely important fact. It does not make sense to have a string
without knowing what encoding it uses.
You can no longer stick your
head in the sand and pretend that "plain" text is ASCII.

This is why you must tell ruby what encoding is used in your file. Since the encoding is not marked in some sort of metadata associated with your file, some software assumed ASCII until it knows better. Ruby 1.9 probably does so until your comment when it will stop, and restart reading the file now decoding it as utf-8.

Obviously, if you used some other Unicode encoding or some more local encoding for your ruby file, you would need to change the comment to indicate the correct encoding.

Ruby on Rails - UTF8 encoding problems in MySQL from ActiveRecord

The problem was linked to my terminal. I imported the language list through it and I think this was the source of my encoding problem.

I downloaded Sequel Pro to manage my database and all of my data are not corrupted.

Avoid using # encoding: UTF-8

You can try setting environment variable RUBYOPT to -Ku value:

export RUBYOPT="-Ku"

Why does Rails 3 pitch a fit about UTF-8 character encoding?

Is it part of changes made to Rails 3.2.1? It is easily fixed by going into Notepad++ and using the Encoding menu option "Convert to UTF-8" but, like I said, I've never had to do this before.

Yes. Rails 3.0+ (I think) requires all templates to be saved in UTF-8 encoding. You need to save the file as UTF-8. If that still doesn't work, set the encoding explicitly by adding on the first line of your .rb files the following:

# encoding: utf-8

Add this to the first line of your .erb templates:

<%# encoding: utf-8 %>

See this related question, and this similar problem. Sounds to me like your editor's encoding settings changed since you originally created the files.

The other odd thing is that even the files that Rails generates are generated with ANSI encoding when I use a generator. Overall, I'm confused and I want to make sure that I'm using good programming practices.

That is quite odd, and I'm not sure I have a good suggestion for that one, other then trying to add Encoding.default_external = "UTF-8" to your config.ru and config/environment.rb files.



Related Topics



Leave a reply



Submit