From: Yusuke ENDOH Date: 2010-01-26T00:21:19+09:00 Subject: [ruby-dev:40149] three bugs of Matrix::Scalar 遠藤です。 Matrix::Scalar の定義に 3 つおかしいところを見つけました。 nodoc なクラスですが、どれも単純な問題だと思うので報告します。 ○ WrongArgType が未定義 $ ./ruby -rmatrix -e 'Matrix[[0]].coerce(1).first + Matrix[[0]]' /home/mame/work/ruby/lib/matrix.rb:979:in `+': uninitialized constant Matrix::Scalar::WrongArgType (NameError) from -e:1:in `
' ただの定義忘れだと思います。 ○ Matrix#powered_by が未定義 $ ./ruby -rmatrix -e '2 ** Matrix[[1]]' /home/mame/work/ruby/lib/matrix.rb:1032:in `**': undefined method `powered_by' for Matrix[[1]]:Matrix (NoMethodError) from -e:1:in `**' from -e:1:in `
' そもそも 2 ** Matrix[[1]] に意味はないと思うので、Matrix::Scalar#** は 引数が Matrix だったら例外にすべきではないでしょうか。 ○ when Numeric の後に when Scalar がある Scalar は Numeric のサブクラスなので、when Scalar は実行されません。 もし実行されるようになったとしても、other.value という記述があり、 Scalar は value というメソッドを提供していないので NoMethodError になる と思います。 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -27,6 +27,7 @@ module ExceptionForMatrix # :nodoc: def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch") def_exception("ErrNotRegular", "Not Regular Matrix") def_exception("ErrOperationNotDefined", "This operation(%s) can\\'t defined") + def_exception("WrongArgType", "wrong argument type %s (expected %s)") end # @@ -977,8 +978,6 @@ class Matrix Scalar.new(@value + other) when Vector, Matrix Scalar.Raise WrongArgType, other.class, "Numeric or Scalar" - when Scalar - Scalar.new(@value + other.value) else x, y = other.coerce(self) x + y @@ -991,8 +990,6 @@ class Matrix Scalar.new(@value - other) when Vector, Matrix Scalar.Raise WrongArgType, other.class, "Numeric or Scalar" - when Scalar - Scalar.new(@value - other.value) else x, y = other.coerce(self) x - y @@ -1029,10 +1026,8 @@ class Matrix case other when Numeric Scalar.new(@value ** other) - when Vector - Scalar.Raise WrongArgType, other.class, "Numeric or Scalar or Matrix" - when Matrix - other.powered_by(self) + when Vector, Matrix + Scalar.Raise WrongArgType, other.class, "Numeric or Scalar" else x, y = other.coerce(self) x ** y -- Yusuke ENDOH