Project

General

Profile

Actions

Feature #6118

closed

Hash#keys_of(values), returns related keys of given values

Added by naruse (Yui NARUSE) over 12 years ago. Updated almost 6 years ago.

Status:
Feedback
Target version:
-
[ruby-dev:45310]

Description

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 ssuda (Sambasiva Suda), 03/27/2012 01:38 AM
000_ruby_hash_keys_of.patch (2.36 KB) 000_ruby_hash_keys_of.patch ssuda (Sambasiva Suda), 03/27/2012 01:44 AM

Updated by ssuda (Sambasiva Suda) over 12 years ago

Here is the implementation for Hash#keys_of

Updated by mame (Yusuke Endoh) over 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by mame (Yusuke Endoh) almost 12 years ago

  • Target version set to 2.6
Actions #5

Updated by naruse (Yui NARUSE) almost 7 years ago

  • Target version deleted (2.6)

Updated by naruse (Yui NARUSE) almost 6 years ago

  • Status changed from Assigned to Feedback

I forgot use case.

use case is welcome

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0