Project

General

Profile

Feature #6118

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

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

Status:
Assigned
Priority:
Normal
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

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

History

#1 [ruby-dev:45438] Updated by ssuda (Sambasiva Suda) over 5 years ago

Here is the implementation for Hash#keys_of

#3 [ruby-dev:45457] Updated by mame (Yusuke Endoh) over 5 years ago

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

#4 [ruby-dev:46563] Updated by mame (Yusuke Endoh) almost 5 years ago

  • Target version set to next minor

Also available in: Atom PDF