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

Implement `fetch_values` for `HashWithIndifferentAccess` #28316

Merged
merged 1 commit into from Apr 10, 2017

Conversation

Projects
None yet
4 participants
@joshpencheon
Contributor

joshpencheon commented Mar 6, 2017

fetch_values was added to Hash in Ruby 2.3.0: https://bugs.ruby-lang.org/issues/10017

This patch adds an implemention for instances of HashWithIndifferentAccess, in line with the existing definitions of fetch and values_at.

Current behaviour, without patch:

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:a] = 'x'
hash[:b] = 'y'
hash.fetch_values('a', 'b') # => KeyError: key not found: "a"

New behaviour, with patch:

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:a] = 'x'
hash[:b] = 'y'
hash.fetch_values('a', 'b') # => ["x", "y"]
hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"]
hash.fetch_values('a', 'c') # => KeyError: key not found: "c"

Thanks, Josh

Show outdated Hide outdated activesupport/lib/active_support/hash_with_indifferent_access.rb
Implement `fetch_values` for HashWithIndifferentAccess
`fetch_values` was added to Hash in Ruby 2.3.0:
  https://bugs.ruby-lang.org/issues/10017

This patch adds an implemention for instances of HWAI, in line
with the existing definitions of `fetch` and `values_at`.
@joshpencheon

This comment has been minimized.

Show comment
Hide comment
@joshpencheon

joshpencheon Apr 9, 2017

Contributor

Thanks @matthewd, I hadn't considered that. I've made the changes you suggested; is there any formal way of documenting that these clauses can be removed once Ruby 2.3 is the minimum requirement?

I've rebased now development has moved to 5.2, and squashed my commits. Is there anything this PR needs?

Contributor

joshpencheon commented Apr 9, 2017

Thanks @matthewd, I hadn't considered that. I've made the changes you suggested; is there any formal way of documenting that these clauses can be removed once Ruby 2.3 is the minimum requirement?

I've rebased now development has moved to 5.2, and squashed my commits. Is there anything this PR needs?

@matthewd

This comment has been minimized.

Show comment
Hide comment
@matthewd

matthewd Apr 10, 2017

Member

Thanks!

is there any formal way of documenting that these clauses can be removed once Ruby 2.3 is the minimum requirement

Nope. I don't think we have enough of them to warrant a formal process... we'll just look for version-relevant conditionals (like method_defined? calls) when the time comes.

Member

matthewd commented Apr 10, 2017

Thanks!

is there any formal way of documenting that these clauses can be removed once Ruby 2.3 is the minimum requirement

Nope. I don't think we have enough of them to warrant a formal process... we'll just look for version-relevant conditionals (like method_defined? calls) when the time comes.

@matthewd matthewd merged commit 2144e70 into rails:master Apr 10, 2017

2 checks passed

codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@sandstrom

This comment has been minimized.

Show comment
Hide comment
@sandstrom

sandstrom Apr 18, 2017

I was just about to send a PR when I saw this! 😄 Great addition @joshpencheon!

sandstrom commented Apr 18, 2017

I was just about to send a PR when I saw this! 😄 Great addition @joshpencheon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment