1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

This commit is contained in:
David Heinemeier Hansson 2015-05-13 19:15:28 +02:00
parent 07ad2a945b
commit e54277a45d
4 changed files with 37 additions and 0 deletions

View file

@ -1,3 +1,8 @@
* 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.

View file

@ -1,3 +1,4 @@
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'

View file

@ -0,0 +1,19 @@
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

View file

@ -27,4 +27,16 @@ class IntegerExtTest < ActiveSupport::TestCase
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