* [ruby-core:121299] [Ruby master Feature#21182] Add Hash#rename
@ 2025-03-11 19:04 calebm (Caleb Meyer) via ruby-core
2025-03-12 5:41 ` [ruby-core:121304] " nobu (Nobuyoshi Nakada) via ruby-core
0 siblings, 1 reply; 2+ messages in thread
From: calebm (Caleb Meyer) via ruby-core @ 2025-03-11 19:04 UTC (permalink / raw)
To: ruby-core; +Cc: calebm (Caleb Meyer)
Issue #21182 has been reported by calebm (Caleb Meyer).
----------------------------------------
Feature #21182: Add Hash#rename
https://bugs.ruby-lang.org/issues/21182
* Author: calebm (Caleb Meyer)
* Status: Open
----------------------------------------
Abstract: Implement Hash#rename which takes as arguments a from key and a to key and renames the from key to the new name.
Background: One of the most common data transformations for a hash (in my experience) is renaming a single key. You can do this with the existing hash methods (e.g. `hash['new'] = hash.delete('old')` or `hash.transform_keys('old' => 'new')`) but they feel a bit clunky.
Proposal:
Add a new method to Hash, something like this:
```ruby
class Hash
# usage { old: 'value' }.rename(:old, to: :new)
# => { new: 'value' }
def rename(from, to:)
transform_keys(from => to)
end
def rename!(from, to:)
transform_keys!(from => to)
end
end
```
--
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] 2+ messages in thread
* [ruby-core:121304] [Ruby master Feature#21182] Add Hash#rename
2025-03-11 19:04 [ruby-core:121299] [Ruby master Feature#21182] Add Hash#rename calebm (Caleb Meyer) via ruby-core
@ 2025-03-12 5:41 ` nobu (Nobuyoshi Nakada) via ruby-core
0 siblings, 0 replies; 2+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2025-03-12 5:41 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #21182 has been updated by nobu (Nobuyoshi Nakada).
`transform_keys('old' => 'new')` feels good enough.
At least, `('old' => 'new')` (or even `('old', 'new')`, `(from: 'old', to: 'new')`) looks clearer than `(:old, to: :new)` to me.
And another concern is if the method name `rename` express the semantics well.
Is there a consensus that `name` means `key`?
----------------------------------------
Feature #21182: Add Hash#rename
https://bugs.ruby-lang.org/issues/21182#change-112266
* Author: calebm (Caleb Meyer)
* Status: Open
----------------------------------------
Abstract: Implement Hash#rename which takes as arguments a from key and a to key and renames the from key to the new name.
Background: One of the most common data transformations for a hash (in my experience) is renaming a single key. You can do this with the existing hash methods (e.g. `hash['new'] = hash.delete('old')` or `hash.transform_keys('old' => 'new')`) but they feel a bit clunky.
Proposal:
Add a new method to Hash, something like this:
```ruby
class Hash
# usage { old: 'value' }.rename(:old, to: :new)
# => { new: 'value' }
def rename(from, to:)
transform_keys(from => to)
end
def rename!(from, to:)
transform_keys!(from => to)
end
end
```
--
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] 2+ messages in thread
end of thread, other threads:[~2025-03-12 5:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-11 19:04 [ruby-core:121299] [Ruby master Feature#21182] Add Hash#rename calebm (Caleb Meyer) via ruby-core
2025-03-12 5:41 ` [ruby-core:121304] " nobu (Nobuyoshi Nakada) 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).