ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:110725] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value?
@ 2022-11-12 16:10 dorianmariefr
  2022-11-13  2:30 ` [ruby-core:110734] " sawa (Tsuyoshi Sawada)
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: dorianmariefr @ 2022-11-12 16:10 UTC (permalink / raw)
  To: ruby-core

Issue #19128 has been reported by dorianmariefr (Dorian Marié).

----------------------------------------
Feature #19128: Hash#delete could take a second argument as the default value?
https://bugs.ruby-lang.org/issues/19128

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
e.g. `{}.delete(:a, [])` would return `[]`



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110734] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value?
  2022-11-12 16:10 [ruby-core:110725] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value? dorianmariefr
@ 2022-11-13  2:30 ` sawa (Tsuyoshi Sawada)
  2022-11-14 10:26 ` [ruby-core:110751] " nobu (Nobuyoshi Nakada)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: sawa (Tsuyoshi Sawada) @ 2022-11-13  2:30 UTC (permalink / raw)
  To: ruby-core

Issue #19128 has been updated by sawa (Tsuyoshi Sawada).


Since you are avoiding doing:

```rb
{}.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?

----------------------------------------
Feature #19128: Hash#delete could take a second argument as the default value?
https://bugs.ruby-lang.org/issues/19128#change-100069

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
e.g. `{}.delete(:a, [])` would return `[]`



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110751] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value?
  2022-11-12 16:10 [ruby-core:110725] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value? dorianmariefr
  2022-11-13  2:30 ` [ruby-core:110734] " sawa (Tsuyoshi Sawada)
@ 2022-11-14 10:26 ` nobu (Nobuyoshi Nakada)
  2022-11-14 12:03 ` [ruby-core:110753] " byroot (Jean Boussier)
  2022-11-24  9:16 ` [ruby-core:110872] " dorianmariefr
  3 siblings, 0 replies; 5+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2022-11-14 10:26 UTC (permalink / raw)
  To: ruby-core

Issue #19128 has been updated by nobu (Nobuyoshi Nakada).


Or
```ruby
{}.delete(:a) {[]}
```

----------------------------------------
Feature #19128: Hash#delete could take a second argument as the default value?
https://bugs.ruby-lang.org/issues/19128#change-100089

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
e.g. `{}.delete(:a, [])` would return `[]`



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110753] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value?
  2022-11-12 16:10 [ruby-core:110725] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value? dorianmariefr
  2022-11-13  2:30 ` [ruby-core:110734] " sawa (Tsuyoshi Sawada)
  2022-11-14 10:26 ` [ruby-core:110751] " nobu (Nobuyoshi Nakada)
@ 2022-11-14 12:03 ` byroot (Jean Boussier)
  2022-11-24  9:16 ` [ruby-core:110872] " dorianmariefr
  3 siblings, 0 replies; 5+ messages in thread
From: byroot (Jean Boussier) @ 2022-11-14 12:03 UTC (permalink / raw)
  To: ruby-core

Issue #19128 has been updated by byroot (Jean Boussier).


@dorianmariefr 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 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.

----------------------------------------
Feature #19128: Hash#delete could take a second argument as the default value?
https://bugs.ruby-lang.org/issues/19128#change-100091

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
e.g. `{}.delete(:a, [])` would return `[]`



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110872] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value?
  2022-11-12 16:10 [ruby-core:110725] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value? dorianmariefr
                   ` (2 preceding siblings ...)
  2022-11-14 12:03 ` [ruby-core:110753] " byroot (Jean Boussier)
@ 2022-11-24  9:16 ` dorianmariefr
  3 siblings, 0 replies; 5+ messages in thread
From: dorianmariefr @ 2022-11-24  9:16 UTC (permalink / raw)
  To: ruby-core

Issue #19128 has been updated by dorianmariefr (Dorian Marié).


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.

----------------------------------------
Feature #19128: Hash#delete could take a second argument as the default value?
https://bugs.ruby-lang.org/issues/19128#change-100230

* Author: dorianmariefr (Dorian Marié)
* Status: Open
* Priority: Normal
----------------------------------------
e.g. `{}.delete(:a, [])` would return `[]`



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-24  9:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-12 16:10 [ruby-core:110725] [Ruby master Feature#19128] Hash#delete could take a second argument as the default value? dorianmariefr
2022-11-13  2:30 ` [ruby-core:110734] " sawa (Tsuyoshi Sawada)
2022-11-14 10:26 ` [ruby-core:110751] " nobu (Nobuyoshi Nakada)
2022-11-14 12:03 ` [ruby-core:110753] " byroot (Jean Boussier)
2022-11-24  9:16 ` [ruby-core:110872] " dorianmariefr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).