[ruby-core:85788] [Ruby trunk Feature#14546] Hash#delete!

From: nobu@...
Date: 2018-02-24 02:20:59 UTC
List: ruby-core #85788
Issue #14546 has been updated by nobu (Nobuyoshi Nakada).


`Hash#delete` is destructive, so appending `!` doesn't seem making sense.
Rather, a destructive version of `Hash#fetch` feels better.

----------------------------------------
Feature #14546: Hash#delete!
https://bugs.ruby-lang.org/issues/14546#change-70637

* Author: rringler (Ryan Ringler)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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.

~~~ ruby
{ 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:

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


---Files--------------------------------
hash_delete_bang.patch (2.37 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next