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

`HashWithIndifferentAccess.new` respects the default value or proc on objects that respond to `#to_hash` #16357

Merged
merged 1 commit into from Oct 29, 2015

Conversation

Projects
None yet
3 participants
@gchan
Contributor

gchan commented Jul 31, 2014

Builds on the work of #12550 where .new will convert the object (that respond to #to_hash) to a hash and add that hash's keys and values to itself.

This change will also make .new respect the default value or proc of objects that respond to #to_hash.
In other words, this .new behaves exactly like .new_from_hash_copying_default.

.new_from_hash_copying_default now simply invokes .new and any references to .new_from_hash_copying_default are replaced with .new.

Should we deprecate .new_from_hash_copying_default now it is consistent with .new?

Added tests confirm behavior.


This PR is a work in progress. Feedback is appreciated.

`HashWithIndifferentAccess.new` respects the default value or proc on…
… objects that respond to `#to_hash`.

Builds on the work of #12550 where `.new` will convert the object (that respond to `#to_hash`) to a hash and
add that hash's keys and values to itself.

This change will also make `.new` respect the default value or proc of objects that respond to `#to_hash`.
In other words, this `.new` behaves exactly like `.new_from_hash_copying_default`.

`.new_from_hash_copying_default` now simply invokes `.new` and any references to `.new_from_hash_copying_default`
are replaced with `.new`.

Added tests confirm behavior.
@gchan

This comment has been minimized.

Contributor

gchan commented Jul 31, 2014

Supporting code as an example:

class Foo
  def to_hash
    hash = Hash.new(3) # or Hash.new{ 1 + 2 }
    hash[:a] = 1
    hash
  end
end

hash_wia = HashWithIndifferentAccess.new(Foo.new)

hash_wia[:a] # => 1 This already works
hash_wia[:b] # => 3 Respects the default value or proc

@sgrif sgrif merged commit 6e574e8 into rails:master Oct 29, 2015

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details

sgrif added a commit that referenced this pull request Oct 29, 2015

Merge pull request #16357 from gchan/hwia-respects-to-hash-default
`HashWithIndifferentAccess.new` respects the default value or proc on
objects that respond to `#to_hash`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment