Ruby steps toward frozen string literals

Beginning with Ruby 3.4.0, string literals in files without a frozen_string_literal comment will behave as if they were frozen.

pile of poorly sorted red ruby crystals
Potapov Alexander / Shutterstock

The next version of the Ruby programming language, Ruby 3.4.0, has been released in preview, bringing changes for string literals and class updates.

Unveiled May 16, the Ruby 3.4.0 preview is downloadable from ruby-lang.org. With this update, string literals in files without a frozen_string_literal comment now behave as if they were frozen. If mutated, a deprecation warning is emitted. The change marks a first step toward making frozen string literals the default in Ruby. Frozen or immutable strings offer both performance and safety advantages.

In other language changes, keyword splatting nil when calling methods now is supported, while block passing and keyword arguments are no longer allowed in indexes.

Ruby 3.4.0 also introduces two core class updates. First, Exception#set_backtrace now accepts arrays of Thread::Backtrace::Location, and Kernel#raise, Thread#raise, and Fiber#raise accept this new format as well. Second, Range#size now raises TypeError if the range is not iterable.

Error messages and backtrace displays have been changed to address compatibility issues. Developers now can use a single quote instead of a backtick as an opening quote. It is also now allowed to display a class name before a method name when the class has a permanent name.

In other improvements, Array#each has been rewritten for better performance. Passing a block to a method that does not use the passed block now will show a warning in verbose mode. And redefining some core methods that are specially optimized by the interpreter and JIT now will emit a performance class warning.

Ruby 3.4.0 follows predecessors including last month’s Ruby 3.2.4 release, which brought security fixes, and the Christmas 2023 Ruby 3.3.0 release, which featured the Prism parser. Ruby itself dates back to a 1995 initial public release.

Copyright © 2024 IDG Communications, Inc.