Feature #19128
closedHash#delete could take a second argument as the default value?
Description
e.g. {}.delete(:a, [])
would return []
Updated by sawa (Tsuyoshi Sawada) about 2 years ago
Since you are avoiding doing:
{}.delete(:a) || []
it must be the case that you have a hash in which nil
can be a meaningful value, and you want to distinguish that from the case where delete
results in a miss hit. What is the use case for that?
Updated by nobu (Nobuyoshi Nakada) about 2 years ago
Or
{}.delete(:a) {[]}
Updated by byroot (Jean Boussier) about 2 years ago
@dorianmariefr (Dorian Marié) if you wish for this feature to be considered, you'll need to expand a bit on your description. Like providing a few snippets of code that show how it is helpful, how often and how it makes your code better. Bonus point if you can link to open source code that would benefit from this.
Once that is said, I personally wanted this a bunch of time. As mentioned by others, in most case hash.delete(:foo) || default
works fine, but sometimes explicit nil
or false
may be expected, in which case you have to use hash.delete(:foo) if hash.key?(:foo)
.
Also as @nobu (Nobuyoshi Nakada) mentioned, Hash#delete
already handle a "default block" like fetch, so I think it would make sense to accept a positional default too, just for consistency's sake.
Updated by dorianmariefr (Dorian Marié) about 2 years ago
You can close this, passing a block is much nicer and provides the functionality I need.
As a side note, I think passing multiple arguments could delete multiple keys from the hash but I don't have the use for it.
Updated by byroot (Jean Boussier) about 2 years ago
- Status changed from Open to Rejected