我正在编写便捷方法来检查数字是正数还是负数,如下所示:
class Numeric
def positive?
self > 0
end
def negative?
self < 0
end
end
但在这种情况下我不知道如何处理这样的情况:
>> 0.positive?
>> 0.negative?
更新:我已经更新了类名中的拼写错误。我使用数字是因为我还需要检查 float 。
最佳答案
ruby 2.3 introduced Numeric#positive?
and Numeric#negative?
,如果你使用的是 Ruby 2.3+,应该改用这两个。
关于ruby - Ruby 的 Number 类和 0 的便捷方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3706756/