【问题标题】:How to get the position of a Key/value pair inside a hash?如何在哈希中获取键/值对的位置?
【发布时间】:2014-02-20 08:49:36
【问题描述】:

下面的代码可以缩短吗?

h = { 1 => "a", 2 => "b", 3 => "c" }
h.to_a.index([2,'b']) # => 1

我在 Hash API 中没有找到任何内容。

【问题讨论】:

    标签: ruby hash enumerable


    【解决方案1】:

    如下使用Enumerable#find_index

    h = { 1 => "a", 2 => "b", 3 => "c" }
    h.find_index([2,'b']) # => 1
    

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 2021-12-30
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2015-01-10
      • 1970-01-01
      相关资源
      最近更新 更多