Issue #14546 has been updated by shevegen (Robert A. Heiler).


> as I understand ! does not mean destructive, but rather
> 'more dangerous', which I believe a possible exception
> qualifies for.

You may still have the issue of semantics.

For example, .fetch() semantic is different than .delete().

The way how I remember methods with a "!", even if this is
not the official one, is to think of the "!" as a "modify
in place" operation. If one thinks about it in this way
then .delete() already modifies in place so .delete!()
is not making a lot of sense. However had, please do not
think that I really mind either way - I am neutral on the
suggestion. Perhaps someone could mention it to matz 
briefly in the next developer meeting since Ryan referred
to a comment from matz almost 10 years ago. :-)

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

* 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:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>