diff options
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/integer.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/integer/inquiry.rb | 19 | ||||
-rw-r--r-- | activesupport/test/core_ext/integer_ext_test.rb | 12 |
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 |