Project

General

Profile

Actions

Feature #14546

open

Hash#delete!

Added by rringler (Ryan Ringler) over 6 years ago. Updated 8 months ago.

Status:
Assigned
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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0