Skip to content

Commit

Permalink
feature: add except to IndifferentHash (#1940)
Browse files Browse the repository at this point in the history
The method `Hash#except` [was added in Ruby
3.0](ruby/ruby@82ca8c7)
so it would be nice that calling `except` in a `Sinatra::IndifferentHash`
supported the indifferent access that the latter provides – right now,
calling `except(:a)` on `Sinatra::IndifferntHash[a: :a]` would not
filter the `a` key.

This commits adds indifferent access support to except so that it will
exclude the passed key independently if the key is passed as a `String`
or `Symbol`.

---------

Co-authored-by: Santiago Rodriguez <santiago.rodriguez@ext.airbnb.com>
Co-authored-by: Eloy Pérez <ej.perezgomez@gmail.com>
  • Loading branch information
3 people committed Aug 22, 2023
1 parent 35bc85b commit 0889424
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/sinatra/indifferent_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def compact
dup.tap(&:compact!)
end

def except(*keys)
keys.map!(&method(:convert_key))

super(*keys)
end if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0")

private

def convert_key(key)
Expand Down
5 changes: 5 additions & 0 deletions test/indifferent_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,9 @@ def test_compact
compacted_hash = non_empty_hash.compact
assert_equal non_empty_hash, compacted_hash
end

def test_except
hash = @hash.except(?b, 3, :simple_nested, 'nested')
assert_equal Sinatra::IndifferentHash[a: :a], hash
end if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.0")
end

0 comments on commit 0889424

Please sign in to comment.