Add Integer#positive? and Integer#negative? query methods in the vein of Fixnum#zero?

上级 07ad2a94
* Add Integer#positive? and Integer#negative? query methods in the vein of Fixnum#zero?
This makes it nicer to do things like bunch_of_numbers.select(&:positive?).
*DHH*
* Encoding ActiveSupport::TimeWithZone to YAML now preserves the timezone information.
Fixes #9183.
......
require 'active_support/core_ext/integer/multiple'
require 'active_support/core_ext/integer/inflections'
require 'active_support/core_ext/integer/inquiry'
require 'active_support/core_ext/integer/time'
class Integer
# Returns true if the number is positive.
#
# 1.positive? # => true
# 0.positive? # => false
# -1.positive? # => false
def positive?
self > 0
end
# Returns true if the number is positive.
#
# -1.positive? # => true
# 0.positive? # => false
# 1.positive? # => false
def negative?
self < 0
end
end
......@@ -27,4 +27,16 @@ def test_ordinal
assert_equal 'st', 1.ordinal
assert_equal 'th', 8.ordinal
end
def test_positive
assert 1.positive?
assert_not 0.positive?
assert_not -1.positive?
end
def test_negative
assert -1.negative?
assert_not 0.negative?
assert_not 1.negative?
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册