From: matz@... (Yukihiro Matsumoto) Date: 2004-03-08T13:20:25+09:00 Subject: Re: Possible strangeness in Numeric Hi, In message "Re: Possible strangeness in Numeric" on 04/03/07, Mathieu Bouchard writes: |Have you tried something like this?: | |class Numeric | def to_int | x=self.to_i | raise "not a representation of an integer" if x!=self | x | end |end | |class Float; remove_method :to_int; end |class Integer; remove_method :to_int; end | |So (2.5).to_int raises, but (2.0).to_int returns 2, and |Rational(4,3).to_int raises, but Rational(6,3).to_int returns 2. Very interesting idea. I'm not sure whether this is what people (who wants float values to integer arguments) want. But still very interesting. |Complex(2.0,0.0).to_int, however, doesn't work, because Complex doesn't |have a #to_f nor a #to_i at all. I am wondering what a good default #to_f |for Complex would be. It wouldn't be modulus because then |Complex(-2.0,0).to_f == +2.0, which would be really bad. Taking the real |part sounds more reasonable. I don't really have any other ideas. We can just raise TypeError Exception when number does not have to_i. matz.