aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2015-05-13 19:15:28 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2015-05-13 19:15:42 +0200
commite54277a45da3c86fecdfa930663d7692fd083daa (patch)
treebfdb6c599930e4a2ab4ba258a207b95c3ad3eda3 /activesupport
parent07ad2a945b6e9ec9421746294bd0b6787fd0ecdd (diff)
downloadrails-e54277a45da3c86fecdfa930663d7692fd083daa.tar.gz
rails-e54277a45da3c86fecdfa930663d7692fd083daa.tar.bz2
rails-e54277a45da3c86fecdfa930663d7692fd083daa.zip
Add Integer#positive? and Integer#negative? query methods in the vein of Fixnum#zero?
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md5
-rw-r--r--activesupport/lib/active_support/core_ext/integer.rb1
-rw-r--r--activesupport/lib/active_support/core_ext/integer/inquiry.rb19
-rw-r--r--activesupport/test/core_ext/integer_ext_test.rb12
4 files changed, 37 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index ac27dc640e..3b905b1d24 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -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.
diff --git a/activesupport/lib/active_support/core_ext/integer.rb b/activesupport/lib/active_support/core_ext/integer.rb
index a44a1b4c74..f5b185b42b 100644
--- a/activesupport/lib/active_support/core_ext/integer.rb
+++ b/activesupport/lib/active_support/core_ext/integer.rb
@@ -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'
diff --git a/activesupport/lib/active_support/core_ext/integer/inquiry.rb b/activesupport/lib/active_support/core_ext/integer/inquiry.rb
new file mode 100644
index 0000000000..dc710b74d0
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/integer/inquiry.rb
@@ -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
diff --git a/activesupport/test/core_ext/integer_ext_test.rb b/activesupport/test/core_ext/integer_ext_test.rb
index 41736fb672..6eeadb2ace 100644
--- a/activesupport/test/core_ext/integer_ext_test.rb
+++ b/activesupport/test/core_ext/integer_ext_test.rb
@@ -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