Re: Multidimensional Arrays

chessboard = Array.new(8) {|idx| Array.new(8, “”)}
chessboard[3][3] = “pawn”

This will do what you expect. Read the documentation for Array.new It
will explain all these subtle nuances.

Blessings,
TwP

Thanks. That works great. I read and tried to understand the
documentation
for Array.new but failed to understand what I read :slight_smile: It’s nice that I
don’t
have to store the allotted dimensions. It returns an error message if
the
first number is out of range. If the second number is out of range, it
makes that
particular array longer.

Now all I have to do is file that information away somewhere when I can
find
it when I need it.

Charles G. – Phoenix, AZ … where you can bake the chill out of your
bones.

U might just use this.

class Array2D < Array

# Inicializamos
def initialize(width, height)
 		@data = Array.new(width) { Array.new(height) }
end

# Definimos el metodo de indexacion para tratarlo como array en C.
def [](x, y)
 		@data[x][y]
end

# Definimos el metodo de asignacion igual que el de indexacion
def []=(x, y, value)
 		@data[x][y] = value
end

end

ChessBoard = Array2D.new(8,8)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs