Skip to content
Go back

Magic Comments in Ruby

Updated:
Magic Comments in Ruby

Note (2025): Magic comments in Ruby remain relevant, but their usage has evolved: # encoding: utf-8 → Legacy. UTF-8 is default in modern Ruby, rarely needed. # frozen_string_literal: true → Still widely used for performance and immutability. May become default in the future. # shareable_constant_value: literal → Modern and important for concurrency (Ractors). # warn_indent: true → Useful in legacy style enforcement but mostly replaced by RuboCop/linters. This post is still useful for developers maintaining older codebases or working with concurrency in newer Ruby versions.

We all know and use comments in code. In Ruby, comments start with the # character—the interpreter ignores everything after # on that line.

# This is a comment

However, magic comments are special directives—placed at the top of a source file—that change how Ruby interprets that file. They must be on the first line, or the second line if the first is a shebang (#!). Magic comments affect only the current file.

Common magic comments

coding / encoding

Ruby’s default source encoding is UTF-8. You can set a different encoding with either coding: or encoding: at the top of the file:

# encoding: UTF-8
# or
# coding: UTF-8

Must appear in the first comment section of the file.


frozen_string_literal

Introduced in Ruby 2.3, this makes all string literals in the file implicitly frozen—as if #freeze had been called—raising RuntimeError if you try to mutate them.

# frozen_string_literal: true

def test
  s = "hello world"
  s.object_id
end

p test  # => e.g., 260
p test  # => same object_id when reused by the VM

Notes:

name = +"hello"   # mutable copy
key = -"api:v1"

shareable_constant_value

Experimental in Ruby 3.0+, this directive helps create constants that hold only immutable (Ractor-shareable) objects. It can take one of:

It can be used multiple times in a file; it affects only subsequent constants and only in the current scope.

See Ruby docs for details on behavior and caveats.


warn_indent

Shows a warning when indentation is mismatched. The last warn_indent: in the file wins.

# warn_indent: true

def comments
  end
# => warning: mismatched indentations at 'end' with 'def' at 3

Running Ruby with -w (warnings) can also surface indentation issues; setting the directive to false suppresses these warnings.


Wrap-up


You might also like


Share this post on:

Previous Post
Recovering a MySQL Root Password
Next Post
Retrieving a Random Row in ActiveRecord