Feature #6118

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

Added by Yui NARUSE about 3 years ago. Updated over 2 years ago.

[ruby-dev:45310]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto

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 Magnifier (2.36 KB) Sambasiva Suda, 03/27/2012 01:38 AM

000_ruby_hash_keys_of.patch Magnifier (2.36 KB) Sambasiva Suda, 03/27/2012 01:44 AM

History

#1 Updated by Sambasiva Suda about 3 years ago

Here is the implementation for Hash#keys_of

#2 Updated by Sambasiva Suda about 3 years ago

Here is the updated patch

#3 Updated by Yusuke Endoh about 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#4 Updated by Yusuke Endoh over 2 years ago

  • Target version set to next minor

Also available in: Atom PDF