ruby 다차원 array에서 element 찾기

lazylady의 이미지

현재 class Point의 인스턴스가 2차원 어레이로 배열된 상황입니다.
@points = Array.new(@@maxX,Array.new(@@maxY,Point.new))
ruby에서 Array.new는 다음과 같습니다.

Array.new  	->  	[]
Array.new(2) 	-> 	[nil, nil]
Array.new(5, "A") 	-> 	["A", "A", "A", "A", "A"]
Array.new(2, Hash.new) 	-> 	[{}, {}]

또한 element의 index 값은 array.index 메소드로 찾을 수 있습니다.

a = [ "a", "b", "c" ]
a.index("b") 	-> 	1
a.index("z") 	-> 	nil

그 외 array 메소드는 다음 링크에 자세히 설명되어 있습니다.http://www.rubycentral.com/ref/ref_c_array.html

질문입니다.

1. 다차원 array에서 element의 index를 구할 수 있는 방법이 있을까요?
2. 제 경우는 2차원 어레이 안에 같은 클래스의 인스턴스가 여러개 배열되어 있는 상황인데, 각각의 element를 구분할 수 있는지 궁금합니다. 예를들어

 a = Point.new("red")
b = Point.new("red")

arr = [a, b]
arr.index(b)                    #요녀석의 값이 무얼까요? 서로 다른 인스턴스이긴 하지만 내용은 같은데요..

잘 아시는 분들 도움 부탁드리겠습니다. 감사합니다.

nohmad의 이미지

일단은 돌아만 가는 소스를 올립니다.

class Point
end

class SentinnelWinder < Exception
  attr_reader :depth, :index
  def initialize(depth, index)
    @depth = depth
    @index = index
  end
end

class Array
  def recursive_index(needle, depth=0)
    self.each_with_index do |e, i|
      if Array === e
        depth += 1
        e.recursive_index(needle, depth)
      else
        depth -= 1 if self.size-1 == i
        raise SentinnelWinder.new(depth, i) if e == needle
      end
    end
  rescue SentinnelWinder => sw
    puts "depth: #{sw.depth}, index: #{sw.index}"
  end
end

x = Point.new
y = Point.new
z = Point.new

p [x, y, z].index(y)
p [x, [z, y], z].flatten.index(y)
[x, [z], [z, [x, z], y]].recursive_index(y)