Indicates which string encoding should be used for string literals,
regexp literals and `__ENCODING__`:
# encoding: big5
''.encoding # => #<Encoding:Big5>
Default encoding is UTF-8.
It must appear in the first comment section of a file
The word "coding" may be used instead of "encoding".
=== <code>frozen_string_literal</code> Directive
When appears in the top section of a file, indicates that string literals should be allocated once at parse time and frozen.
# frozen_string_literal: true
3.times do
p 'hello'.object_id # => prints same number
end
p 'world'.frozen? # => true
The default is false; this can be changed with `--enable=frozen-string-literal`.
Without the directive, or with <code># frozen_string_literal: false</code>,
the example above would print 3 different numbers and "false".
Starting in Ruby 3.0, string literals that are dynamic are not frozen nor reused:
# frozen_string_literal: true
p "Addition: #{2 + 2}".frozen? # => false
It must appear in the first comment section of a file
=== <code>warn_indent</code> Directive
This directive can turn detection of bad indentation for statements that follow it:
def foo
end # => no warning
# warn_indent: true
def bar
end # => warning: mismatched indentations at 'end' with 'def' at 6
Another way to get these warnings to show is by running Ruby with warnings (<code>ruby -w</code>). Using a directive to set this false will prevent these warnings to show.
Note: This directive is experimental in Ruby 3.0 and may change in future releases.
This special directive helps to create constants that hold only immutable objects, or {Ractor-shareable}[rdoc-ref:Ractor@Shareable+and+unshareable+objects] constants.
The directive can specify special treatment for values assigned to constants:
* +none+: (default)
* +literal+: literals are implicitly frozen, others must be Ractor-shareable
* +experimental_everything+: all made shareable
==== Mode +none+ (default)
No special treatment in this mode (as in Ruby 2.x): no automatic freezing and no checks.
It has always been a good idea to deep-freeze constants; Ractor makes this
an even better idea as only the main ractor can access non-shareable constants:
# shareable_constant_value: none
A = {foo: []}
A.frozen? # => false
Ractor.new { puts A } # => can not access non-shareable objects by non-main Ractor.
==== Mode +literal+
In "literal" mode, constants assigned to literals will be deeply-frozen:
# shareable_constant_value: literal
X = [{foo: []}] # => same as [{foo: [].freeze}.freeze].freeze
Other values must be shareable:
# shareable_constant_value: literal
X = Object.new # => cannot assign unshareable object to X
Note that only literals directly assigned to constants, or recursively held in such literals will be frozen:
# shareable_constant_value: literal
var = [{foo: []}]
var.frozen? # => false (assignment was made to local variable)
X = var # => cannot assign unshareable object to X
X = Set[1, 2, {foo: []}].freeze # => cannot assign unshareable object to X
# (`Set[...]` is not a literal and
# `{foo: []}` is an argument to `Set.[]`)
The method Module#const_set is not affected.
==== Mode +experimental_everything+
In this mode, all values assigned to constants are made shareable.