From: janosch84@... Date: 2018-05-30T20:24:04+00:00 Subject: [ruby-core:87309] [Ruby trunk Feature#14546] Hash#delete! Issue #14546 has been updated by janosch-x (Janosch M��ller). duerst (Martin D��rst) wrote: > 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). I'm not sure that point holds in such a general way. E.g. I've never seen `ActiveRecord::Base#destroy` being mistaken for a non-destructive version of `#destroy!`. In my opinion, the main reason why `#delete!` feels confusing is that there are already `#delete` and `#delete!` implementations in Ruby that differ by destructiveness, e.g. on String. That is an unfortunate inconsistency when compared to `Hash#delete`. Maybe something like `Hash#yank` could be introduced as an alias for delete and `#yank!` as a whiny version. That could both serve the need stated by the OP and also undercut this existing inconsistency. ---------------------------------------- Feature #14546: Hash#delete! https://bugs.ruby-lang.org/issues/14546#change-72305 * 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: