ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal
@ 2024-09-13  7:33 ursm (Keita Urashima) via ruby-core
  2024-09-13  7:57 ` [ruby-core:119170] " nobu (Nobuyoshi Nakada) via ruby-core
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-13  7:33 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been reported by ursm (Keita Urashima).

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119170] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
@ 2024-09-13  7:57 ` nobu (Nobuyoshi Nakada) via ruby-core
  2024-09-13 11:59 ` [ruby-core:119172] " osyo (manga osyo) via ruby-core
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2024-09-13  7:57 UTC (permalink / raw)
  To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)

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


"A special value" doesn't feel like a good idea to me.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109748

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119172] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
  2024-09-13  7:57 ` [ruby-core:119170] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2024-09-13 11:59 ` osyo (manga osyo) via ruby-core
  2024-09-13 12:16 ` [ruby-core:119173] " ursm (Keita Urashima) via ruby-core
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: osyo (manga osyo) via ruby-core @ 2024-09-13 11:59 UTC (permalink / raw)
  To: ruby-core; +Cc: osyo (manga osyo)

Issue #20738 has been updated by osyo (manga osyo).


How about using `**`?

```ruby
def bar? = false

{
  foo: 1,
  **(bar? ? { bar: 2 } : {})
}
# => {:foo=>1}
```

Also, you can use `**nil` in ruby 3.4-dev.

```ruby
def bar? = false

{
  foo: 1,
  **({ bar: 2 } if bar?)
}
# => {:foo=>1}
```

see:
* [Feature #18959: Handle gracefully nil kwargs eg. **nil - Ruby master - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/18959)
* [Bug #20064: Inconsistent behavior between array splat *nil and hash splat **nil - Ruby master - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/20064)


----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109750

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119173] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
  2024-09-13  7:57 ` [ruby-core:119170] " nobu (Nobuyoshi Nakada) via ruby-core
  2024-09-13 11:59 ` [ruby-core:119172] " osyo (manga osyo) via ruby-core
@ 2024-09-13 12:16 ` ursm (Keita Urashima) via ruby-core
  2024-09-13 13:17 ` [ruby-core:119174] " ursm (Keita Urashima) via ruby-core
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-13 12:16 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


Yes, I sometimes do that as well. However, I am not happy that the shape of the resulting hash is unclear.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109751

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119174] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (2 preceding siblings ...)
  2024-09-13 12:16 ` [ruby-core:119173] " ursm (Keita Urashima) via ruby-core
@ 2024-09-13 13:17 ` ursm (Keita Urashima) via ruby-core
  2024-09-13 14:58 ` [ruby-core:119176] " ursm (Keita Urashima) via ruby-core
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-13 13:17 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


nobu (Nobuyoshi Nakada) wrote in #note-1:
> "A special value" doesn't feel like a good idea to me.

Hmmm, does that mean we should extend the syntax? For example, something like this?

``` ruby
{
  foo: 1,
  ?bar: nil
} #=> {foo: 1}
```

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109752

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119176] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (3 preceding siblings ...)
  2024-09-13 13:17 ` [ruby-core:119174] " ursm (Keita Urashima) via ruby-core
@ 2024-09-13 14:58 ` ursm (Keita Urashima) via ruby-core
  2024-09-14  4:53 ` [ruby-core:119189] " mame (Yusuke Endoh) via ruby-core
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-13 14:58 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


With the previous idea, I can't have both removing entries and returning nil depending on the condition.

``` ruby
# If user.child? is true and user.parent is nil, I want parent_name: nil, but the entry is removed.
{
  ?parent_name: user.child? ? user.parent&.name : nil
}
```

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109753

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119189] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (4 preceding siblings ...)
  2024-09-13 14:58 ` [ruby-core:119176] " ursm (Keita Urashima) via ruby-core
@ 2024-09-14  4:53 ` mame (Yusuke Endoh) via ruby-core
  2024-09-14  5:24 ` [ruby-core:119190] " ursm (Keita Urashima) via ruby-core
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2024-09-14  4:53 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

Issue #20738 has been updated by mame (Yusuke Endoh).


History. Long ago in Ruby, such a special value actually existed. It was `nil`.

```ruby
# ruby 1.4.6
h = { 1 => 2 }

p h  #=> {1=>2}

# This removes the key 1
h[1] = nil

p h  #=> {}
```

However, there were more and more cases where we want to treat `nil` as an ordinary value. Finally, `nil` has lost this speciality.

Considering this history, the special value is not a good idea.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109773

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119190] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (5 preceding siblings ...)
  2024-09-14  4:53 ` [ruby-core:119189] " mame (Yusuke Endoh) via ruby-core
@ 2024-09-14  5:24 ` ursm (Keita Urashima) via ruby-core
  2024-09-14 16:34 ` [ruby-core:119191] " Eregon (Benoit Daloze) via ruby-core
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-14  5:24 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


I believe that the following two points will prevent the same problems as in the past:

1. Use a value that is never used (e.g., Hash::DROP) instead of nil.
2. Special treatment of “special value” only if the hash is constructed with literals.

``` ruby
{
  foo: Hash::DROP
} #=> {}

h = {}
h[:foo] = Hash::DROP
h #=> {foo: Hash::DROP}
```

Note that I am not concerned with the “special value” approach. If there is a better way, please let me know.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109774

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119191] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (6 preceding siblings ...)
  2024-09-14  5:24 ` [ruby-core:119190] " ursm (Keita Urashima) via ruby-core
@ 2024-09-14 16:34 ` Eregon (Benoit Daloze) via ruby-core
  2024-09-15 12:36 ` [ruby-core:119197] " ursm (Keita Urashima) via ruby-core
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-09-14 16:34 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20738 has been updated by Eregon (Benoit Daloze).


I don't think it's OK to magically drop entries from a literal based on some value, it's way too surprising.
And of course `Hash::DROP` could leak into some variable and then unintentionally drop an entry, that'd be horrible to debug.
Notably this would make the capacity of the literal potentially wrong, etc.

```ruby
{
  foo: 1,
  **({ bar: 2 } if bar?)
}
# OR
h[:bar] = 2 if bar?
```
Sounds good enough to me.

I don't think it's very frequent to need this to warrant a syntax change either.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109776

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119197] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (7 preceding siblings ...)
  2024-09-14 16:34 ` [ruby-core:119191] " Eregon (Benoit Daloze) via ruby-core
@ 2024-09-15 12:36 ` ursm (Keita Urashima) via ruby-core
  2024-09-15 16:32 ` [ruby-core:119198] " jeremyevans0 (Jeremy Evans) via ruby-core
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-15 12:36 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


Eregon (Benoit Daloze) wrote in #note-8:
> I don't think it's very frequent to need this to warrant a syntax change either.

There are several Rails codes that can be improved with this feature. In my opinion, it is especially useful in multiple situations, such as generating JSON responses and constructing HTTP request headers. It would be more useful if it could be applied to keyword arguments as well.

- https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L1121
- https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb#L201
- https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L1141
- https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb#L19
- https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb#L31
- https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb#L16

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109781

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119198] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (8 preceding siblings ...)
  2024-09-15 12:36 ` [ruby-core:119197] " ursm (Keita Urashima) via ruby-core
@ 2024-09-15 16:32 ` jeremyevans0 (Jeremy Evans) via ruby-core
  2024-09-15 18:06 ` [ruby-core:119202] " byroot (Jean Boussier) via ruby-core
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2024-09-15 16:32 UTC (permalink / raw)
  To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)

Issue #20738 has been updated by jeremyevans0 (Jeremy Evans).


ursm (Keita Urashima) wrote in #note-9:
> There are several Rails codes that can be improved with this feature. In my opinion, it is especially useful in multiple situations, such as generating JSON responses and constructing HTTP request headers. It would be more useful if it could be applied to keyword arguments as well.
> 
> - https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L1121
> - https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb#L201
> - https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L1141
> - https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb#L19
> - https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb#L31
> - https://github.com/rails/rails/blob/6d1252cf3e65a7720aad5511ff719b44e49fd2a3/activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb#L16

I reviewed every example listed and in all cases I think it would be made harder to understand using the proposed feature.  Each example listed is straightforward and easy to understand currently.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109782

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119202] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (9 preceding siblings ...)
  2024-09-15 16:32 ` [ruby-core:119198] " jeremyevans0 (Jeremy Evans) via ruby-core
@ 2024-09-15 18:06 ` byroot (Jean Boussier) via ruby-core
  2024-09-16  1:19 ` [ruby-core:119205] " ursm (Keita Urashima) via ruby-core
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2024-09-15 18:06 UTC (permalink / raw)
  To: ruby-core; +Cc: byroot (Jean Boussier)

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


> There are several Rails codes that can be improved with this feature. 

As one of the maintainer of the code you linked to, I agree with @jeremyevans0 that it's currently fine as it is. I wouldn't use such feature if it was added.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109784

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119205] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (10 preceding siblings ...)
  2024-09-15 18:06 ` [ruby-core:119202] " byroot (Jean Boussier) via ruby-core
@ 2024-09-16  1:19 ` ursm (Keita Urashima) via ruby-core
  2024-09-16  1:35 ` [ruby-core:119206] " ursm (Keita Urashima) via ruby-core
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-16  1:19 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


I would like to offer that as I used a simple grep pattern, I could only find simple examples. I wanted to show that this is not as rare as it seems.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109787

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119206] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (11 preceding siblings ...)
  2024-09-16  1:19 ` [ruby-core:119205] " ursm (Keita Urashima) via ruby-core
@ 2024-09-16  1:35 ` ursm (Keita Urashima) via ruby-core
  2024-10-03  7:12 ` [ruby-core:119422] " matz (Yukihiro Matsumoto) via ruby-core
  2024-10-08 23:30 ` [ruby-core:119484] " ursm (Keita Urashima) via ruby-core
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-09-16  1:35 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


It would be better to explain the motive. This is an appropriate code.

``` ruby
{
  foo: 1
  bar: 2
}
```

This is not a mistake, but it's a circuitous code.

``` ruby
h = {}
h[:foo] = 1
h[:bar] = 2
h
```

The gist of this proposal is that if a condition is included, it should be written in the same form as the “appropriate code”.

``` ruby
h = {}
h[:foo] = 1
h[:bar] = 2 if bar?
h
```


----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-109788

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119422] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (12 preceding siblings ...)
  2024-09-16  1:35 ` [ruby-core:119206] " ursm (Keita Urashima) via ruby-core
@ 2024-10-03  7:12 ` matz (Yukihiro Matsumoto) via ruby-core
  2024-10-08 23:30 ` [ruby-core:119484] " ursm (Keita Urashima) via ruby-core
  14 siblings, 0 replies; 16+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2024-10-03  7:12 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #20738 has been updated by matz (Yukihiro Matsumoto).


I don't want to add a special value (Hash::DROP) nor special syntax (?key:) here. Use `h = {foo: 1}; h[:bar] = 2 if bar?`.

Matz.


----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-110036

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:119484] [Ruby master Feature#20738] Removing a specific entry from a hash literal
  2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
                   ` (13 preceding siblings ...)
  2024-10-03  7:12 ` [ruby-core:119422] " matz (Yukihiro Matsumoto) via ruby-core
@ 2024-10-08 23:30 ` ursm (Keita Urashima) via ruby-core
  14 siblings, 0 replies; 16+ messages in thread
From: ursm (Keita Urashima) via ruby-core @ 2024-10-08 23:30 UTC (permalink / raw)
  To: ruby-core; +Cc: ursm (Keita Urashima)

Issue #20738 has been updated by ursm (Keita Urashima).


OK, I'm sorry to hear that, but I'm glad to hear your opinions. Thanks.

----------------------------------------
Feature #20738: Removing a specific entry from a hash literal
https://bugs.ruby-lang.org/issues/20738#change-110101

* Author: ursm (Keita Urashima)
* Status: Open
----------------------------------------
Sometimes I want to decide whether or not to add a particular entry to a hash depending on a condition. If the entire hash does not use nil, I can use Hash#compact.

```ruby
{
  foo: 1,
  bar: bar? ? 2 : nil
}.compact
```

But if I want to remove only a specific entry while leaving the other nil, it is somewhat cumbersome. I have to either assign the hash once and change it destructively, or use Hash#reject.

``` ruby
h = {
  foo: 1,
  baz: nil
}

h[:bar] = 2 if bar?
```
``` ruby
{
  foo: 1,
  bar: bar? ? 2 : :drop,
  baz: nil
}.reject {|_, v| v == :drop }
```

As a suggestion, how about a special value that indicates an invalid key for the hash? With this, the above example could be written like this:

``` ruby
{
  foo: 1,
  bar: bar? ? 2 : Hash::DROP,
  baz: nil
} #=> {foo: 1, baz: nil}
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

end of thread, other threads:[~2024-10-08 23:31 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-13  7:33 [ruby-core:119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal ursm (Keita Urashima) via ruby-core
2024-09-13  7:57 ` [ruby-core:119170] " nobu (Nobuyoshi Nakada) via ruby-core
2024-09-13 11:59 ` [ruby-core:119172] " osyo (manga osyo) via ruby-core
2024-09-13 12:16 ` [ruby-core:119173] " ursm (Keita Urashima) via ruby-core
2024-09-13 13:17 ` [ruby-core:119174] " ursm (Keita Urashima) via ruby-core
2024-09-13 14:58 ` [ruby-core:119176] " ursm (Keita Urashima) via ruby-core
2024-09-14  4:53 ` [ruby-core:119189] " mame (Yusuke Endoh) via ruby-core
2024-09-14  5:24 ` [ruby-core:119190] " ursm (Keita Urashima) via ruby-core
2024-09-14 16:34 ` [ruby-core:119191] " Eregon (Benoit Daloze) via ruby-core
2024-09-15 12:36 ` [ruby-core:119197] " ursm (Keita Urashima) via ruby-core
2024-09-15 16:32 ` [ruby-core:119198] " jeremyevans0 (Jeremy Evans) via ruby-core
2024-09-15 18:06 ` [ruby-core:119202] " byroot (Jean Boussier) via ruby-core
2024-09-16  1:19 ` [ruby-core:119205] " ursm (Keita Urashima) via ruby-core
2024-09-16  1:35 ` [ruby-core:119206] " ursm (Keita Urashima) via ruby-core
2024-10-03  7:12 ` [ruby-core:119422] " matz (Yukihiro Matsumoto) via ruby-core
2024-10-08 23:30 ` [ruby-core:119484] " ursm (Keita Urashima) via ruby-core

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 NNTP newsgroup(s).