How do I find .index of a multidimensional array

后端 未结 7 1683
-上瘾入骨i
-上瘾入骨i 2021-02-08 19:34

Tried web resources and didnt have any luck and my visual quick start guide.

If I have my 2d/multidimensional array:

 array = [[\'x\', \'x\',\' x\',\'x\'         


        
7条回答
  •  面向向阳花
    2021-02-08 20:14

    array = [['x', 'x',' x','x'],
             ['x', 'S',' ','x'],
             ['x', 'x',' x','x']]
    class Array
      def my_index item
        self.each_with_index{|raw, i| return i if raw.include? item}
        return
      end
    end
    
    p array.my_index("S") #=>1
    p array.my_index("Not Exist Item") #=> nil
    

提交回复
热议问题