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 now.
Run your tests
with the warnings on:
RUBYOPT="-W:deprecated" bundle exec rspec
Every warning
is a line
that will break.
Or rehearse the future
in a branch:
RUBYOPT="--enable-frozen-string-literal"
Now every literal
is frozen,
and you get the FrozenError
today,
with a stack trace,
instead of later.
Fix each one
by asking for
a string you can change:
out = +""
Unary plus
returns a mutable copy.
Careful with String.new
without an argument.
It gives you a binary string,
not UTF-8,
and that has its own surprises.
Then add the magic comment
to each file you fix,
so it already behaves
the way the future will.
Gems you depend on
need the same check.
Run the suite
with the flag,
and read the warnings
from their paths too.
A deprecation you cannot see
is not a quiet one.
It is a countdown
with the sound off.
– Serguey Asael Shinder
Top comments (0)