Allow to dump instance variables on non-primitive binary strings:#774
Open
Edouard-chin wants to merge 1 commit intoruby:masterfrom
Open
Allow to dump instance variables on non-primitive binary strings:#774Edouard-chin wants to merge 1 commit intoruby:masterfrom
Edouard-chin wants to merge 1 commit intoruby:masterfrom
Conversation
- I'd like to propose the feature to dump instance variables when
dumpin a binary string created from a String subclass.
One of the the use case I have is in Rails for the PostgreSQL
adapter where we need to make sure that we aren't escaping the
result twice. We decode the result and set an instance variable
on the string to tell that it was decoded so that we later
don't redo-this.
The problem is that when we dump that string, we lose this
information since Psych don't dump ivars for binary strings.
Psych already allows to dump ivars on strings for string subclass.
In this patch, it will now be possible to create a String subclass,
and when it gets dumped it will retain ivars.
A behaviour worth highlighting is that the subclass information
isn't retained. When the dumped string gets loaded, a `String`
object is created with the previously dumped ivars set.
Prior to this patch, this is already the case so there is no
behaviour difference.
```ruby
class MyString < String
end
Psych.load(Psych.dump(MyString.new("\xE2".b))) => String object
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Allow to dump instance variables on non-primitive binary strings:
Hello 👋
I'd like to propose the feature to dump instance variables when dumpin a binary string created from a String subclass.
One of the the use case I have is in Rails for the PostgreSQL adapter where we need to make sure that we aren't escaping the result twice. We decode the result and set an instance variable on the string to tell that it was decoded so that we later don't redo-this.
The problem is that when we dump that string, we lose this information since Psych don't dump ivars for binary strings.
Psych already allows to dump ivars on strings for string subclass. In this patch, it will now be possible to create a String subclass, and when it gets dumped it will retain ivars.
A behaviour worth highlighting is that the subclass information isn't retained. When the dumped string gets loaded, a
Stringobject is created with the previously dumped ivars set. Prior to this patch, this is already the case so there is no behaviour difference.