[ruby-dev:50758] [Ruby trunk Feature#6118][Feedback] Hash#keys_of(values), returns related keys of given values
From:
naruse@...
Date:
2019-01-10 08:16:08 UTC
List:
ruby-dev #50758
Issue #6118 has been updated by naruse (Yui NARUSE).
Status changed from Assigned to Feedback
I forgot use case.
use case is welcome
----------------------------------------
Feature #6118: Hash#keys_of(values), returns related keys of given values
https://bugs.ruby-lang.org/issues/6118#change-76194
* Author: naruse (Yui NARUSE)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
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 (2.36 KB)
--
https://bugs.ruby-lang.org/