根据子哈希值获取数组索引

说我有这个:

[ { :id => 34, :votes_count => 3 }, { :id => 2, :votes_count => 0 }, ] 

如何根据id获取索引? 我想要做的是当我搜索id: 34时返回0 ,当我搜索id: 2 。 什么是最有效的方式?

您可以将块传递给#index

 array.index {|h| h[:id] == 34 } # => 0