I’m writing convenience methods to check if number is positive or negative like so:
class Numeric
def positive?
self > 0
end
def negative?
self < 0
end
end
but in this case I do not know how to handle cases like these:
>> 0.positive?
>> 0.negative?
Update: I’ve updated the typo in the class name. I used numeric because I needed to check the floats as well.
If the problem is that you’re getting
falsefor both, either you consider0to be positive or not. If so, you should have something like:If not, leave it as it is, since
0is neither positive not negative and you should return false for both.However if the problem is that you’re getting errors with
0.positive?(far more likely), the reason you’re getting a problem is because0is aFixNum, not aNumber. You can see that with the following message:You should probably add it to
Fixnumitself, orInteger, orNumeric, the base class for various numeric types likeFixNumandBigNum. Where you inject your convenience methods depends on how widely you want them available.For example, if you change your code to the following (I’m including test code here):
it then works fine, outputting:
as expected.