Project

General

Profile

Actions

Feature #19128

closed

Hash#delete could take a second argument as the default value?

Added by dorianmariefr (Dorian Marié) over 1 year ago. Updated over 1 year ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:110725]

Description

e.g. {}.delete(:a, []) would return []

Updated by sawa (Tsuyoshi Sawada) over 1 year 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) over 1 year ago

Or

{}.delete(:a) {[]}

Updated by byroot (Jean Boussier) over 1 year 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é) over 1 year 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.

Actions #5

Updated by byroot (Jean Boussier) over 1 year ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0Like0