Fix ArgumentError on escaping frozen String #7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thank you @condor for creating this great gem! It's very convenient to format our application logs, but I have an idea that it would be even better if it supported frozen String values in its
#dump
implementation.Problem
I use ltsv gem to format our chronological logs in our Rails application like this:
Expected behavior
I think it should return a String like
"foo:bar\ttime:2018-11-22 15:10:34 +0900"
without any exception.Actual behavior
But unfortunately the code above raises ArgumentError. This is because its implementation tries to mutate the given String by using
String#gsub!
to escape special characters.Note: Time.current.to_s (ActiveSupport::TimeWithZone#to_s) returns a frozen String (since Rails 5.2.0), and
LTSV#escape
uses it.ltsv/lib/ltsv.rb
Lines 134 to 139 in f545450
Solution
To fix the problem above, I replaced
String#gsub!
withString#gsub
.I want to think there is no special reason to mutate given String here, but I'm afraid there might be a performance reason to choose
String#gsub!
. What do you think @condor?