What this thread does
Do first: replace a literal used as an append buffer with String.new, or take an unfrozen copy before String#<< mutates it.
Short answer
As of November 2026, Ruby 4.0.9 was released October 23, 2026. Attempting to modify a frozen object raises FrozenError. Keep the policy enabled and allocate that buffer explicitly instead of disabling freezing across the application (String, FrozenError).
Choose / don't choose
- Immutable text: choose
# frozen_string_literal: truein the first comment section (comments). - Fresh accumulator: choose
String.new(String construction). - Mutable copy of existing text: choose
dupfor an unfrozen copy. Don't assume defaultcloneremoves frozen state (Object copying).
Not for: Ruby regexp security patching; see ruby-regexp-compiler-gitlab-cves-upgrade-rbenv-asdf-docker.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
frozen_string_literal | true; put the magic comment in the file's initial comment section, before executable code; first line is the clearest placement | Ruby 4.0 docs | comments |
--enable=frozen-string-literal | Change the default with --enable=frozen-string-literal | Ruby 4.0 docs | directive and CLI switch |
String.new | No argument creates an empty string; default encoding ASCII-8BIT; pass an existing string or explicit encoding when required | Ruby 4.0 API | constructor |
String#+@ | Unary + yields an unfrozen string; duplicates a frozen receiver, but may return the same already mutable string | Ruby 4.0 API | String |
dup / clone | dup gives a shallow unfrozen copy; default clone preserves frozen state | Ruby 4.0 API | Object |
The directive affects only the source file where it appears, not files it requires. The CLI switch changes the default, while explicit file directives remain local. Dynamically interpolated literals are not frozen/reused under this directive starting with Ruby 3.0 (file and dynamic scope). Dave Schweisguth's explanation is historical context.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
can't modify frozen String | The example appends with << to a frozen literal | Use String.new or duplicate the text before mutation | dated explanation, current exception, String |
| Frozen state after copying | clone includes the source's frozen state | Use dup for a shallow unfrozen copy | Object copy semantics, FrozenError |
Missing
- The linked sources confirm: a unary-plus result can alias a mutable input, so use dup when independent ownership is required. Neither operation deep-copies arbitrary nested structures (Object, String).
- No output encoding contract is specified. String.new's binary default and an empty literal's script encoding are different; choose deliberately for text versus byte buffers.
- This covers explicit frozen-literal policy; it does not establish every runtime's warning behavior when the directive is omitted.
Checked
2026-11-12; Ruby 4.0.9 release listing, Ruby 4.0 comment/String/Object/FrozenError docs and Dave Schweisguth's dated explanation read. No Ruby commands executed.
Started from a post by @Ruby, packaged by the editor. Original post