ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: naruse@airemix•jp
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50758] [Ruby trunk Feature#6118][Feedback] Hash#keys_of(values), returns related keys of given values
Date: Thu, 10 Jan 2019 08:16:08 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76194.20190110081607.4618400fb435afd0@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-6118.20120306175507@ruby-lang.org

Issue #6118 has been updated by naruse (Yui NARUSE).

Status changed from Assigned to Feedback

I forgot use case.

use case is welcome

----------------------------------------
Feature #6118: Hash#keys_of(values), returns related keys of given values
https://bugs.ruby-lang.org/issues/6118#change-76194

* Author: naruse (Yui NARUSE)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
Hash#key(value) の複数版がほしいです。

{a: 1, b: 2, c: 3, d: 1}.key(1)
=> :a
というメソッドはあるのですが、
{a: 1, b: 2, c: 3, d: 1}.keys_of(1)
=> [:a, :d]
というメソッドは現状ありません。

Ruby での実装例は以下のような感じになります。
どうでしょうか。

class Hash
  def keys_of(*a)
    each_with_object([]) {|(k, v), r| r << k if a.include? v}
  end
end

---Files--------------------------------
000_ruby_hash_keys_of.patch (2.36 KB)
000_ruby_hash_keys_of.patch (2.36 KB)


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

      parent reply	other threads:[~2019-01-10  8:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-06  8:55 [ruby-dev:45310] [ruby-trunk - Feature #6118][Open] Hash#keys_of(values), returns related keys of given values Yui NARUSE
2012-03-26 16:38 ` [ruby-dev:45438] [ruby-trunk - Feature #6118] " ssuda (Sambasiva Suda)
2012-03-26 16:44 ` [ruby-dev:45439] " ssuda (Sambasiva Suda)
2012-03-29 16:54 ` [ruby-dev:45457] [ruby-trunk - Feature #6118][Assigned] " mame (Yusuke Endoh)
2012-11-20 13:18 ` [ruby-dev:46563] [ruby-trunk - Feature #6118] " mame (Yusuke Endoh)
2019-01-10  8:16 ` naruse [this message]

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-list from there: mbox

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

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

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

  git send-email \
    --in-reply-to=redmine.journal-76194.20190110081607.4618400fb435afd0@ruby-lang.org \
    --to=ruby-dev@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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 read-only IMAP folder(s) and NNTP newsgroup(s).