Skip to content

Commit

Permalink
Merge pull request #49475 from fatkodima/hwia-to_proc
Browse files Browse the repository at this point in the history
Implement `HashWithIndifferentAccess#to_proc`
  • Loading branch information
rafaelfranca committed Oct 4, 2023
2 parents 536d168 + 3943831 commit d21f4e6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,2 +1,8 @@
* Implement `HashWithIndifferentAccess#to_proc`.

Previously, calling `#to_proc` on `HashWithIndifferentAccess` object used inherited `#to_proc`
method from the `Hash` class, which was not able to access values using indifferent keys.

*fatkodima*

Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/activesupport/CHANGELOG.md) for previous changes.
Expand Up @@ -387,6 +387,10 @@ def to_hash
_new_hash
end

def to_proc
proc { |key| self[key] }
end

private
if Symbol.method_defined?(:name)
def convert_key(key)
Expand Down
9 changes: 9 additions & 0 deletions activesupport/test/hash_with_indifferent_access_test.rb
Expand Up @@ -965,4 +965,13 @@ def non_hash.to_hash
assert_equal :bar, hash_wia[:foo]
assert_equal :baz, hash_wia[:missing]
end

def test_indifferent_to_proc
@strings = @strings.with_indifferent_access
proc = @strings.to_proc

assert_equal 1, proc["a"]
assert_equal 1, proc[:a]
assert_nil proc[:no_such]
end
end

0 comments on commit d21f4e6

Please sign in to comment.