如果获取索引,如果数组中的对象包含Ruby中的期望值

Kar*_*ter 1 ruby

所以这是我从某个地方获得的数组,我想得到包含"text"=>"Assets"的对象的索引.我怎么能在Ruby中做到这一点?

[{
                     "class" => "android.support.v7.widget.AppCompatTextView",
                       "tag" => nil,
               "description" => "android.support.v7.widget.AppCompatTextView{b777d8c V.ED..... ........ 0,39-355,168 #7f0e007f app:id/textview_sectiontitle}",
                        "id" => "textview_sectiontitle",
                      "text" => "Assets",
                   "visible" => true,
                      "rect" => {
              "height" => 129,
               "width" => 355,
                   "y" => 2088,
                   "x" => 80,
            "center_x" => 257,
            "center_y" => 2152
        },
                   "enabled" => true,
        "contentDescription" => nil
    },
    {
                     "class" => "android.support.v7.widget.AppCompatImageButton",
                       "tag" => nil,
               "description" => "android.support.v7.widget.AppCompatImageButton{ae57704 VFED..C.. ........ 1056,0-1280,208 #7f0e0109 app:id/imagebutton_amount}",
                        "id" => "imagebutton_amount",
                   "visible" => true,
                      "rect" => {
              "height" => 208,
               "width" => 224,
                   "y" => 2049,
                   "x" => 1136,
            "center_x" => 1248,
            "center_y" => 2153
        },
                   "enabled" => true,
        "contentDescription" => nil
    }]
Run Code Online (Sandbox Code Playgroud)

Urs*_*sus 6

试试这个吧

arr.index { |item| item["text"] == "Assets" }
Run Code Online (Sandbox Code Playgroud)