Comments vs. Magic Comments

In Ruby, you can annotate and document your code using 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.

Here, the # encoding: big5 is interpreted by Ruby.

When Ruby reads this magic comment it automatically sets the encoding of any string declared in this file to Encoding:Big5 — we’ll dive deeper into encoding: in the last section of this article.

Now that we know the difference between a comment and a magic comment, let’s have a quick look at magic comment’s specifications.