Skip to content
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

Receiver hash modified when using non-bang #deep_merge #26

Open
owst opened this issue Oct 26, 2016 · 3 comments
Open

Receiver hash modified when using non-bang #deep_merge #26

owst opened this issue Oct 26, 2016 · 3 comments

Comments

@owst
Copy link

owst commented Oct 26, 2016

Given that the Hash#merge method does not modify the receiver hash, I think it's really confusing that Hash#deep_merge does.

For#merge:

irb --simple-prompt merge.rb
>> h = {}
=> {}
>> h.merge(m: 1)
=> {:m=>1}
>> h
=> {}
>> h.merge!(m!: 2)
=> {:m!=>2}
>> h
=> {:m!=>2}

but for #deep_merge:

irb --simple-prompt deep_merge.rb
>> require 'deep_merge'
=> true
>> h = {}
=> {}
>> h.deep_merge(dm: 1)
=> {:dm=>1}
>> h
=> {:dm=>1}
>> h.deep_merge!(dm!: 2)
=> {:dm=>1, :dm!=>2}
>> h
=> {:dm=>1, :dm!=>2}

I see that #deep_merge! does have different behaviour for unmergeables, but I wonder if instead of using a ! suffix, is should instead use a prefix, in a similar manner to ko_deep_merge!, for consistency within this library.

Overall though, I think consistency with the stdlib is important, and therefore #deep_merge should not modify the receiver hash. As a test, I think this should not raise (as it currently does) {}.freeze.deep_merge(a: 1).

@santib
Copy link

santib commented Oct 28, 2016

I agree with @owst. I opened a PR, in case the maintainers are fine with the change we also need to change the README and probably other docs too.

@Fryguy
Copy link
Collaborator

Fryguy commented Jan 7, 2022

I agree with this one as well, but it will require a major version bump due to a change in signature. We'd also have to come up with new methods for the existing split between preserve_unmergeables => true and false (or perhaps just change the default to false).

@gregorbg
Copy link

gregorbg commented Oct 10, 2023

I just stumbled into this problem and it prompted us to add an extra deep_dup in our codebase. It works but is hacky, any chance of getting a proper fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants