Menu

Your Ruby Code Appends to an Empty String Literal and Ruby Is Getting Ready to Freeze It
📰

Your Ruby Code Appends to an Empty String Literal and Ruby Is Getting Ready to Freeze It

DEV Community: ruby·Serguey Asael Shinder·10 days ago
#qm7M1dFt
#devtoruby#devto#dev#string#frozen
Long article (321 words) — no AI summary yet.
Reading 0:00
15s threshold

The export

builds a CSV

the old way.

def to_csv(rows)

out = ""

rows.each { |r| out << r.join(",") << "\n" }

out

end

An empty string.

Append,

append,

append.

It has run

every night

for nine years.

Here is what is changing.

Ruby has been moving

towards frozen string literals

for a long time.

Most files in Rails apps

already start with:

frozen_string_literal: true

Files without it

have always been allowed

to change their literals.

Ruby 3.4

changed that quietly.

A literal

in a file without the comment

is now "chilled".

It still works.

But mutate it,

with << or gsub!,

and Ruby

records a deprecation:

warning: literal string will be frozen in the future

The plan is

that a later Ruby

freezes them for real.

Then the same line

raises FrozenError.

can't modify frozen String

Here is the trap.

Deprecation warnings

are off by default.

So the nightly export

says nothing.

The test suite

says nothing.

Everything is green

until the version

where it stops.

Here is what to do…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.