在 ruby​​ 中,如何选择第一个哈希条目,其键是数组的第一个匹配条目?

psy*_*ave 1 arrays hash select ruby-on-rails actioncontroller

目标类似于以下代码:

h={ i:4, j:3, k:2}
a=[ :f, :g, :j, :z, :i]
h.get_first_matching_in(a)
=> :j
h.select first_from(a)
=> :j
Run Code Online (Sandbox Code Playgroud)

什么应该进去.get_first_matching_infirst_from

Ily*_*lya 5

我会这样写:

(a & h.keys).first
 => :j
Run Code Online (Sandbox Code Playgroud)

或者:

a.find { |e| h[e] }
Run Code Online (Sandbox Code Playgroud)