From: argento / pearl.ocn.ne.jp Subject: [ruby-list:41489] Matrixの継承について Date: Sun, 6 Nov 2005 21:17:31 +0900 るびきちです。 > Matrixクラスを継承したクラスを作ろうとしているのですが、 > > require 'matrix' > > class YaMatrix < Matrix > def new_method_for_yamatrix > end > end Rubyの場合、無理に継承を使う必要はありません。 どういう目的でしょうか? ただ共通のメソッドを持っていれば、同じオブジェクトみたいに扱えるのです。 StringIOクラスはIOのサブクラスではありませんが、同じメソッドを持っているので、 IOオブジェクトを置き換えて使えます。 new_method_for_yamatrix を導入するだけなら、Matrixに追加するか、 moduleを作ってincludeしてしまえばいいです。 class Matrix def new_method_for_yamatrix end end -- module MatrixExtension def new_method_for_yamatrix end end class Matrix include MatrixExtension end もし、特定の行列のみ new_method_for_yamatrix メソッドを持たせたければ、 extendしてしまいましょう。 m = Matrix[[1,2],[3,4]] m.extend MatrixExtension > def Matrix.identity(n) > Matrix.scalar(n, 1) > end こうなっている以上、現在の仕様だと思っています。 るびきち☆ http://www.rubyist.net/~rubikitch/