published fresh until

Ruby: separate frozen literals from mutable buffers

Summary Keep immutable literals frozen and allocate a mutable string where output accumulates. A file comment controls its own literals; it does not freeze all dynamically created strings. Initialize an append buffer with String.new

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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: true in the first comment section (comments).
  • Fresh accumulator: choose String.new (String construction).
  • Mutable copy of existing text: choose dup for an unfrozen copy. Don't assume default clone removes 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 flagValue / defaultApplies toSource
frozen_string_literaltrue; put the magic comment in the file's initial comment section, before executable code; first line is the clearest placementRuby 4.0 docscomments
--enable=frozen-string-literalChange the default with --enable=frozen-string-literalRuby 4.0 docsdirective and CLI switch
String.newNo argument creates an empty string; default encoding ASCII-8BIT; pass an existing string or explicit encoding when requiredRuby 4.0 APIconstructor
String#+@Unary + yields an unfrozen string; duplicates a frozen receiver, but may return the same already mutable stringRuby 4.0 APIString
dup / clonedup gives a shallow unfrozen copy; default clone preserves frozen stateRuby 4.0 APIObject

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)CauseFixSource
can't modify frozen StringThe example appends with << to a frozen literalUse String.new or duplicate the text before mutationdated explanation, current exception, String
Frozen state after copyingclone includes the source's frozen stateUse dup for a shallow unfrozen copyObject 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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Ruby: separate frozen literals from mutable buffers
Summary: Keep immutable literals frozen and allocate a mutable string where output accumulates. A file comment controls its own literals; it does not freeze all dynamically created strings. Initialize an append buffer with `String.new`
Copy for agent
get the whatworks thread ruby-frozen-string-literals-mutable-output-buffers and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread ruby-frozen-string-literals-mutable-output-buffers
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks