Skip to content

Allow to dump instance variables on non-primitive binary strings: - #774

Open
Edouard-chin wants to merge 1 commit into
ruby:masterfrom
Shopify:ec-ivars-binary-string
Open

Allow to dump instance variables on non-primitive binary strings:#774
Edouard-chin wants to merge 1 commit into
ruby:masterfrom
Shopify:ec-ivars-binary-string

Conversation

@Edouard-chin

@Edouard-chinEdouard-chin commented Feb 4, 2026

Copy link
Copy Markdown
Member

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 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.

classMyString < StringendPsych.load(Psych.dump(MyString.new("\xE2".b))).class# => String

- 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
```
Sign up for freeto 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.

1 participant

@Edouard-chin