Project

General

Profile

Feature #14546

Hash#delete!

Added by rringler (Ryan Ringler) over 2 years ago. Updated over 2 years ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:85782]

Description

Hash#delete currently returns nil if a given key is not found in the hash. It would be nice to have a way to check that the key was present in the hash. This can be accomplished with with a block, but it would be nice to have some sugar for this.

{ a: 'a' }.delete(:b)                                                  # => nil
{ a: 'a' }.delete(:b) { |key| raise KeyError, "key not found #{key}" } # => KeyError (key not found: b)

I'd like to propose a Hash#delete! method:

{ a: 'a' }.delete!(:a) # => 'a'
{ a: 'a' }.delete!(:b) # => KeyError (key not found: :b)

Files

hash_delete_bang.patch (2.37 KB) hash_delete_bang.patch rringler (Ryan Ringler), 02/23/2018 06:47 PM

Also available in: Atom PDF