From: Aron Griffis Date: 2002-04-25T00:24:27+09:00 Subject: Beginner's question Hello, I'm attempting to implement a game called "Twixt". There are lots of pages on the web describing the game, for example Google's first hit is http://www.flash.net/~markthom/html/twixt.html I've read through some of the archives of this list and found an implementation of a Matrix class that seems to meet my needs. Here's how I'm trying to use it: class Matrix < Array def initialize(height,width) super(height) self.each_index { |row| self[row] = Array.new(width) self[row].each_index { |col| self[row][col] = yield row, col } } end end class TwixtPeg RED = 0 BLACK = 1 def initialize(color) @color = color @barriers = [] end attr :color end class TwixtBoard < Matrix def initialize(width = 24) super(width, width) { nil } end def to_s self.collect {|row| "#{row}"}.join "\n" end def []=(column, row, color) self[row][column] = TwixtPeg.new(color) end def [](column, row) self[row][column].color end end board = TwixtBoard.new print board, "\n" tb[0,0] = RED tb[23,23] = BLACK print board, "\n" When I attempt to execute this code, I get the following error: ./mat.rb:5:in `[]=': wrong # of arguments(2 for 3) (ArgumentError) from ./mat.rb:5:in `initialize' from ./mat.rb:4:in `each_index' from ./mat.rb:4:in `initialize' from ./mat.rb:24:in `initialize' from ./mat.rb:40:in `new' from ./mat.rb:40 The problem appears to be that my overridden []= operator is propagating back into the Matrix class. I'm sure this is a beginner's error, but what am I doing wrong? I realize I could also implement this using a Matrix as an instance variable in the TwixtBoard, for example: class TwixtBoard def initialize(width = 24) @m = Matrix.new(24, 24) { nil } end def []=(column, row, color) @m[row][column].color = TwixtPeg.new(color) end end However I don't understand why my first attempt isn't successful. Any suggestions are welcome! Thanks, Aron