Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the magic comment ( # Encoding: utf-8 ) in ruby​​ work?

Tags:

ruby

encoding

How does the magic comment in ruby​​ works? I am talking about:

# Encoding: utf-8 

Is this a preprocessing directive? Are there other uses of this type of construction?

like image 726
Leszek Andrukanis Avatar asked Jan 16 '12 11:01

Leszek Andrukanis


People also ask

What are magic comments?

To declare a comment we use the # character followed by our comment. Here, all the text after the # is not interpreted by Ruby. It only helps the developer to understand the code. On the other hand, there are some comments that are interpreted by Ruby. They are known as magic comments, or magic instructions.

What are magic comments in Ruby?

Magic Comments. While comments are typically ignored by Ruby, special “magic comments” contain directives that affect how the code is interpreted. Top-level magic comments must appear in the first comment section of a file. NOTE: Magic comments affect only the file in which they appear; other files are unaffected.

How do you comment out a block of code in Ruby?

Single line comments in a Ruby script are defined with the '#' character. For example, to add a single line comment to a simple script: # This is a comment line - it explains that the next line of code displays a welcome message print "Welcome to Ruby!"

How do you comment in rails?

The Ruby single-line comment begins with the # character and ends at the end of the line. Any characters from the # character to the end of the line are completely ignored by the Ruby interpreter. The # character doesn't necessarily have to occur at the beginning of the line; it can occur anywhere.


2 Answers

For some reason people refer to this line as magic comment. Before processing your source code interpreter reads this line and sets proper encoding. It's quite common for interpreted languages I believe. At least Python uses the same approach.

You can specify encoding in a number of different ways (some of them are recognized by editors):

# encoding: UTF-8 # coding: UTF-8 # -*- coding: UTF-8 -*- 

You can read some interesting stuff about source encoding in this article.

The only thing I'm aware of that has similar construction is shebang, but it is related to Unix shells in general and is not Ruby-specific.

like image 70
KL-7 Avatar answered Oct 06 '22 12:10

KL-7


This magic comment tells Ruby the source encoding of the currently parsed file. As Ruby 1.9.x by default assumes US_ASCII you have tell the interpreter what encoding your source code is in if you use non-ASCII characters (like umlauts or accented characters).

The comment has to be the first line of the file (or below the shebang if used) to be recognized.

There are other encoding settings. See this question for more information.

Since version 2.0, Ruby assumes UTF-8 encoding of the source file by default. As such, this magic encoding comment has become a rarer sight in the wild if you write your source code in UTF-8 anyway.

like image 20
Holger Just Avatar answered Oct 06 '22 12:10

Holger Just