Skip to content

Conversation

@gwhilts
Copy link
Contributor

@gwhilts gwhilts commented Jan 22, 2025

The following warning came up when running ptools in Ruby 3.4.1:

"...literal string will be frozen in the future"

It appears that in the future literal String assignments will be automatically frozen. (See https://bugs.ruby-lang.org/issues/20205). To avoid these warnings and prepare for the future, I changed the assignment of three dynamic Strings to use String.new() instead.

Warnings no longer appear. All unit tests pass.

The following warning came up when running ptools
in Ruby 3.4.1:

"...literal string will be frozen in the future"

It appears that Ruby will in the future literal
String assignments will be automatically frozen.
(See https://bugs.ruby-lang.org/issues/20205). To
avoid these warnings and prepare for the future, I
changed the assignment of three dynamic Strings to
use String.new() instead.

Warnings no longer appear. All unit tests pass.
@djberg96
Copy link
Owner

Thanks for the report!

@djberg96 djberg96 merged commit cf245b0 into djberg96:main Feb 27, 2025
22 checks passed
@dastra-mak
Copy link

This got broken by 574ebb3.
As a result ruby 3.4 will print warnings: gems/ptools-1.5.1/lib/ptools.rb:433: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information)

@djberg96
Copy link
Owner

djberg96 commented Dec 3, 2025

How is this warning manifesting for you? I don't see it when running specs or in irb.

@dastra-mak
Copy link

This occured in the context of a rails project. A model has the following method:

  def binary?
    File.binary?(file.path)
  end

This is called in a before_validation and results in the warning:

/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:433: warning: literal string will be frozen in the future
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:433: info: the string was created here
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:439: warning: literal string will be frozen in the future
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:439: info: the string was created here
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:468: warning: literal string will be frozen in the future
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:468: info: the string was created here
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:468: warning: literal string will be frozen in the future
/home/user/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/ptools-1.5.1/lib/ptools.rb:468: info: the string was created here

I got the warning with ruby 3.4.1 and 3.4.7.

I think that it is problematic to force encoding on a frozen string. To work around this, you could extract a constants that create byte-encoded copies of the literals, e.g.:

JPEG_JFIF_HEADER = "\xFF\xD8\xFF\xE0\x00\x10JFIF".b.freeze

@djberg96
Copy link
Owner

djberg96 commented Dec 4, 2025

@dastra-mak #56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants