From: Tadayoshi Funaba Date: 2008-02-15T19:39:52+09:00 Subject: [ruby-dev:33792] aFixnum ** aComplex rational を読んでいる場合、以下のようなエラーになります。 $ ruby19 -r rational -r complex -e '1 ** Complex(1)' /usr/local/ruby19/lib/ruby/1.9.0/rational.rb:520:in `rpower': undefined method `>=' for Complex(1, 0):Complex (NoMethodError) from -e:1:in `
' リリースされた 1.9.0 は少し違いますが、基本的に同じです。 $ ruby190 -r rational -r complex -e '1 ** Complex(1)' /usr/local/ruby190/lib/ruby/1.9.0/rational.rb:489:in `>=': super: no superclass method `<=>' for Complex(1, 0):Complex (NoMethodError) from /usr/local/ruby190/lib/ruby/1.9.0/rational.rb:489:in `rpower' from -e:1:in `
' r9634 で、<=> がなくなり、ついこの間 >= などもなくなったことが理由です。 1.8 では、おそらく互換性への配慮から <=> が放置されているためでしょう。 問題がありません。 とりあえず、coerce するようにしてみました。 Index: lib/rational.rb =================================================================== --- lib/rational.rb (revision 15487) +++ lib/rational.rb (working copy) @@ -517,6 +517,10 @@ # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0). def rpower (other) + if defined?(Complex) && Complex === other + x, y = other.coerce(self) + return x ** y + end if other >= 0 self.power!(other) else @@ -537,6 +541,10 @@ # Returns a Rational number if the result is in fact rational (i.e. +other+ < 0). def rpower (other) + if defined?(Complex) && Complex === other + x, y = other.coerce(self) + return x ** y + end if other >= 0 self.power!(other) else