Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I can specify any ruby file to use specific encoding by add a comment line at its top:

#encoding: utf-8 

But in Rails' config/application.rb, I found this:

config.encoding = "utf-8" 

Are they different? If I have set config.encoding = "utf-8", still I need #encoding: utf-8?

like image 417
Lai Yu-Hsuan Avatar asked Oct 08 '11 18:10

Lai Yu-Hsuan


People also ask

Is Ruby on Rails scripting language?

Rails is also open-source, but it is not a scripting language. Ruby on Rails is a web development framework that is based on model view controller (MVC) architecture.

Is Ruby on Rails backend or frontend?

9. Ruby On Rails Covers Front And Back-End. This language is pretty unique in that it covers both the front- and backend, meaning that as a Ruby on Rails developer you can describe yourself as truly full stack.

Is Ruby on Rails open source?

Ruby on Rails (sometimes RoR) is the most popular open-source web application framework. It's built with the Ruby programming language. You can use Rails to help you build applications, from simple to complex, there are no limits to what you can accomplish with Rails!


1 Answers

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.

like image 147
Frost Avatar answered Sep 27 '22 16:09

Frost