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

后端 未结 4 1242
囚心锁ツ
囚心锁ツ 2020-11-28 08:54

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

# Encoding: utf-8

Is this a preprocessing directive? Are there other uses o

4条回答
  •  [愿得一人]
    2020-11-28 09:27

    As you noted, magic comments are a special preprocessing construct. They must be defined at the top of the file (except, if there is already a unix shebang at the top). As of Ruby 2.3 there are three kinds of magic comments:

    • Encoding comment: See other answers. Must always be the first magic comment. Must be ASCII compatible. Sets the source encoding, so you will run into problems if the real encoding of the file does not match the specified encoding
    • frozen_string_literal: true: Freezes all string literals in the current file
    • warn_indent: true: Activates indentation warnings for the current file

    More info: Magic Instructions

提交回复
热议问题