Project

General

Profile

Feature #6118

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

Added by Yui NARUSE almost 4 years ago. Updated about 3 years ago.

Status:
Assigned
Priority:
Normal
[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 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 [ruby-dev:45438] Updated by Sambasiva Suda almost 4 years ago

Here is the implementation for Hash#keys_of

#2 [ruby-dev:45439] Updated by Sambasiva Suda almost 4 years ago

Here is the updated patch

#3 [ruby-dev:45457] Updated by Yusuke Endoh almost 4 years ago

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

#4 [ruby-dev:46563] Updated by Yusuke Endoh about 3 years ago

  • Target version set to next minor

Also available in: Atom PDF