Float#integer?とRational#integer?って変じゃね?
self が整数の時、真を返します。そうでない場合に false を返します。
Numeric#integer?
Numeric のサブクラスは、このメソッドを適切に再定義しなければなりません。
「クラスが整数の時」ではなく「整数の時」に真を返すなら、
require "rational" p 1.0.integer? #=> false p 1.quo(1).integer? #=> false
は間違いではないのかな?*1 数学よくわからんけど。
でも影響範囲とか怖いし……
と思ってruby1.9のライブラリを「integer?」でgrepしてみたら何と0件!
if !elm[1].kind_of?(Integer) and elm[1].denominator != 1
mathn.rb 79行目付近
return Float(self) ** other2
end
ですよねー^^*2
Numeric#integer?の空気っぷりに全俺が泣いた
追記
Complexクラスで落ちたw
p Complex(1).integer? #=> true p Complex(1, 1).integer? #=> can't convert 1+1i into Integer (RangeError)
修正版はこちら
class Numeric def integer? self == to_i rescue false end end
p Complex(1).integer? #=> true p Complex(1, 1).integer? #=> false
オッケーェィ!