An IntegerMatrix Class

Throughout this chapter we'll be building an IntegerMatrix class. Our matrices will hold numbers in rectangular grids of arbitrary sizes and allow certain kinds of math to be carried out on them.

It so happens that Ruby already comes with a sophisticated Matrix class. It lives in the matrix.rb module and is found by a script containing the line require 'matrix' or load 'matrix.rb'. Our IntegerMatrix will be much different, and will not pretend to be a functional approximation of Matrix.

Incidentally, there would be no name conflict if we called our class Matrix as well, so long as it was in a file that was not named matrix.rb and it was used only by scripts that didn't use the other kind of matrix.

Initialization

The first ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.