-
Notifications
You must be signed in to change notification settings - Fork 31
support for forzen string literals #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
support for forzen string literals #8
Conversation
Issue When running tests with Ruby 3.4.0rc1, the following warnings are displayed. ```console $ rake test Run options: --seed 46514 /work/ruby/json-stream/lib/json/stream/parser.rb:549: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information) /work/ruby/json-stream/lib/json/stream/parser.rb:555: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information) ``` Cuases According to https://bugs.ruby-lang.org/issues/20205, string literals may be frozen by default in future Ruby versions (potentially Ruby 4.0+). Starting from Ruby 3.4.0, warnings like `warning: literal string will be frozen in the future` are shown if string literals are mutable. Solution We explicitly make string literals mutable using `+"string literal"` Additionally, we add `# frozen_string_literal: true` at the top of the affected files and handle the warnings to ensure compatibility with Ruby versions before 3.4.0.
lib/json/stream/buffer.rb
Outdated
| # Avoid state machine for complete UTF-8. | ||
| if @buffer.empty? | ||
| data.force_encoding(Encoding::UTF_8) | ||
| (+data).force_encoding(Encoding::UTF_8) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work?
a = "a"
a.force_encoding("ASCII-8BIT")
a.freeze
(+a).force_encoding(Encoding::UTF_8)
p a.encoding # -> #<Encoding:BINARY (ASCII-8BIT)>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it doesn't work. We should assign the result to new variable. I will fix it.
a = "a"
a.force_encoding("ASCII-8BIT")
a.freeze
b = (+a).force_encoding(Encoding::UTF_8)
p a.encoding # -> #<Encoding:BINARY (ASCII-8BIT)>
p b.econding # -> #<Encoding:UTF-8>There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dup than + (conditional dup) may be better here because users may not expect that << may change the argument (data).
data = data.dup
data.force_encoding(Encoding::UTF_8)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: d97d4d2 I see. It totally makes sense to me too!
Issue
When running tests with Ruby 3.4.0rc1, the following warnings are displayed.
Causes
According to https://bugs.ruby-lang.org/issues/20205, string literals may be frozen by default in future Ruby versions (potentially Ruby 4.0+).
Starting from Ruby 3.4.0, warnings like
warning: literal string will be frozen in the futureare shown if string literals are mutable.Solution
We explicitly make string literals mutable using
+"string literal"Additionally, we add# frozen_string_literal: trueat the top of the affected files and handle the warnings to ensure compatibility with Ruby versions before 3.4.0.