Actions
Feature #6118
closedHash#keys_of(values), returns related keys of given values
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
Updated by ssuda (Sambasiva Suda) over 12 years ago
Here is the implementation for Hash#keys_of
Updated by ssuda (Sambasiva Suda) over 12 years ago
Here is the updated patch
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
Updated by naruse (Yui NARUSE) almost 6 years ago
- Status changed from Assigned to Feedback
I forgot use case.
use case is welcome
Actions
Like0
Like0Like0Like0Like0Like0Like0