Skip to content

Commit e54277a

Browse files
author
David Heinemeier Hansson
committed
Add Integer#positive? and Integer#negative? query methods in the vein of Fixnum#zero?
1 parent 07ad2a9 commit e54277a

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

activesupport/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add Integer#positive? and Integer#negative? query methods in the vein of Fixnum#zero?
2+
This makes it nicer to do things like bunch_of_numbers.select(&:positive?).
3+
4+
*DHH*
5+
16
* Encoding ActiveSupport::TimeWithZone to YAML now preserves the timezone information.
27

38
Fixes #9183.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'active_support/core_ext/integer/multiple'
22
require 'active_support/core_ext/integer/inflections'
3+
require 'active_support/core_ext/integer/inquiry'
34
require 'active_support/core_ext/integer/time'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Integer
2+
# Returns true if the number is positive.
3+
#
4+
# 1.positive? # => true
5+
# 0.positive? # => false
6+
# -1.positive? # => false
7+
def positive?
8+
self > 0
9+
end
10+
11+
# Returns true if the number is positive.
12+
#
13+
# -1.positive? # => true
14+
# 0.positive? # => false
15+
# 1.positive? # => false
16+
def negative?
17+
self < 0
18+
end
19+
end

activesupport/test/core_ext/integer_ext_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ def test_ordinal
2727
assert_equal 'st', 1.ordinal
2828
assert_equal 'th', 8.ordinal
2929
end
30+
31+
def test_positive
32+
assert 1.positive?
33+
assert_not 0.positive?
34+
assert_not -1.positive?
35+
end
36+
37+
def test_negative
38+
assert -1.negative?
39+
assert_not 0.negative?
40+
assert_not 1.negative?
41+
end
3042
end

0 commit comments

Comments
 (0)