Note: As of Ruby 3.0.0, Hash#except
is now [part][1] of the language. However, Ruby does not implement Hash#except!
.
Sometimes I want to remove a specific key-value pair from a Ruby hash and get the resulting hash back. If you're using Rails or ActiveSupport you can accomplish this using Hash#except
:
hash = { a: 1, b: 2, c: 3 }
hash.except(:a) # => { b: 2, c: 3 }
# note, the original hash is not modified
hash # => { a: 1, b: 2, c: 3 }