Skip to content

Eagerly defrost chilled strings #93

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/stringio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "stringio.so"

if "test".frozen? && !"test".equal?("test") # Ruby 3.4+ chilled strings
class StringIO
alias_method :_initialize, :initialize
private :_initialize

def initialize(*args, **kwargs)
string = args.first
if string && string.frozen?
begin
string << "" # Eagerly defrost the string
rescue FrozenError
end
end
_initialize(*args, **kwargs)
end
end
end
8 changes: 8 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -981,4 +981,12 @@ def test_coderange_after_overwrite
def assert_string(content, encoding, str, mesg = nil)
assert_equal([content, encoding], [str, str.encoding], mesg)
end

if eval(%{ "test".frozen? && !"test".equal?("test") }) # Ruby 3.4+ chilled strings
def test_chilled_string
io = eval(%{ StringIO.new("") })
io << "test"
assert_equal("test", io.string)
end
end
end