From: Paul Brannan Date: 2002-04-25T00:56:57+09:00 Subject: Re: Beginner's question On Thu, Apr 25, 2002 at 12:24:27AM +0900, Aron Griffis wrote: > 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 Here, you have defined your own [] and []= operators that have a different interface than the base class's [] and []=. If the base class implements any methods in terms of these two methods, then your code will break. (In Ruby, all methods are virtual; when a base class calls a method, Ruby searches for that method starting with the most derived class). A TwixtBoard, therefore, isn't a Matrix, and probably shouldn't inherit from it. If you do want to inherit from Matrix anyway, then you should use: def []=(col, row, color) super(row)[col] = ... 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 And, in fact, the base class does use []=; Matrix#initialize uses it, and crashes when it tries to pass 2 arguments to a method that is expecting 3. Hope this helps, Paul