Issue #14546 has been updated by duerst (Martin Drst).


marcandre (Marc-Andre Lafortune) wrote:
> As Matz clearly stated, even if a long time ago, bang is not strictly for mutating versions of methods. Note that there is `Process#exit!` (which is not a mutating version of `Process#exit`). Rails also has many bang methods which aren't mutating, in particular `ActiveRecord#save!`.

It's clear that bang methods are not only for mutating versions of methods. However, it would be a bad idea to use a bang method in a context (such as delete) where it can be very easily mistaken as a mutating version (and by opposition, the non-bang method would be misunderstood as a non-mutating version). That's why I think it's a bad idea to use `delete!` in the sense proposed above.


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

* Author: rringler (Ryan Ringler)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* 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>