ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "nobu (Nobuyoshi Nakada) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "nobu (Nobuyoshi Nakada)" <noreply@ruby-lang.org>
Subject: [ruby-core:119333] [Ruby master Feature#20769] Add `Hash#transform_value`
Date: Sun, 29 Sep 2024 02:51:12 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-109945.20240929025111.50315@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20769.20240928213407.50315@ruby-lang.org>

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


```ruby
hash.merge(image: nil) {|_, url| download(url)}

hash.merge!(image: nil) {|_, url| download(url)}
```

----------------------------------------
Feature #20769: Add `Hash#transform_value`
https://bugs.ruby-lang.org/issues/20769#change-109945

* Author: seanpdoyle (Sean Doyle)
* Status: Open
----------------------------------------
Add `Hash#transform_value` as a specialized, key-specific version of [Hash#transform_values](https://docs.ruby-lang.org/en/3.3/Hash.html#method-i-transform_values).

```ruby
hash = { image: "https://example.com/image.jpg" }
mutated_hash = hash.transform_value(:image) { |url| download(url) }

hash # => { image: "https://example.com/image.jpg" }
mutated_hash # => { image: File<...> }

hash.transform_value!(:image) { |url| download(url) }
hash # => { image: File<...> }
```

Similar value transformation can be achieved through variable assignment and direct mutation:

```ruby
hash = { image: "https://example.com/image.jpg" }

hash.merge(image: download(hash[:image]))

hash[:image] = download(hash[:image])
```

While simple and currently supported, it requires a local variable (and therefore poses some challenges when chaining other methods) and repeats the Hash key in both the reading and writing portions of the code.



-- 
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/

  reply	other threads:[~2024-09-29  2:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-28 21:34 [ruby-core:119332] " seanpdoyle (Sean Doyle) via ruby-core
2024-09-29  2:51 ` nobu (Nobuyoshi Nakada) via ruby-core [this message]
2024-09-29 14:27 ` [ruby-core:119334] " seanpdoyle (Sean Doyle) via ruby-core
2024-10-05  6:13 ` [ruby-core:119458] " matz (Yukihiro Matsumoto) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-109945.20240929025111.50315@ruby-lang.org \
    --to=ruby-core@ml.ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).