Ruby 如何引用三维阵列的一部分?

Ruby 如何引用三维阵列的一部分?,ruby,multidimensional-array,reference,max,Ruby,Multidimensional Array,Reference,Max,假设我有一个3D数组,我按如下方式推送值: @h = [[[]]] @h.push(["cat", "mammal", 100.0]) @h.push(["lizard", "reptile", 300.0]) @h.push(["dog", "mammal", 200.0]) 我如何通过第三个元素(float)引用max索引,然后引用索引的每个单独元素来输出本例中的值“爬虫” 我试过: @h.each do |(x,y,z)| if ( [x,y,z]

假设我有一个3D数组,我按如下方式推送值:

    @h = [[[]]]
    @h.push(["cat", "mammal", 100.0])
    @h.push(["lizard", "reptile", 300.0])
    @h.push(["dog", "mammal", 200.0])
我如何通过第三个元素(float)引用max索引,然后引用索引的每个单独元素来输出本例中的值“爬虫”

我试过:

@h.each do |(x,y,z)|
    if ( [x,y,z] == @h.max_by{|(x,y,z)| z} )
       puts y
    end
end
但它并不是只给我“y”值


谢谢

主要疼痛区域,您已移除-即
[[[]]
@h = []   # not [[[]]]
@h.push(["cat", "mammal", 100.0])
@h.push(["lizard", "reptile", 300.0])
@h.push(["dog", "mammal", 200.0])
max_ar = @h.max_by{|ar| ar[2]}
p max_ar[1] #=> "reptile"

# Your way is less usual. It works fine, but you're working too hard:

max_ar = @h.max_by{|(x,y,z)| z} 
p max_ar[1]  #=> "reptile"