From ed83f90d6124c4185a6d31010770e3c84d6be5fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20Emin=20=C4=B0NA=C3=87?= Date: Wed, 13 May 2015 21:03:51 +0300 Subject: Move Integer#positive? and Integer#negative? query methods to Numeric class By this way Integer, Rational, Float, Fixnum, Bignum classes have the same behaviour --- activesupport/CHANGELOG.md | 4 ++++ activesupport/lib/active_support/core_ext/integer.rb | 1 - .../lib/active_support/core_ext/integer/inquiry.rb | 19 ------------------- activesupport/lib/active_support/core_ext/numeric.rb | 1 + .../lib/active_support/core_ext/numeric/inquiry.rb | 19 +++++++++++++++++++ activesupport/test/core_ext/integer_ext_test.rb | 12 ------------ activesupport/test/core_ext/numeric_ext_test.rb | 12 ++++++++++++ 7 files changed, 36 insertions(+), 32 deletions(-) delete mode 100644 activesupport/lib/active_support/core_ext/integer/inquiry.rb create mode 100644 activesupport/lib/active_support/core_ext/numeric/inquiry.rb (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 3b905b1d24..4a4992886e 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Move Integer#positive? and Integer#negative? query methods to Numeric class + + *Mehmet Emin İNAÇ* + * 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?). diff --git a/activesupport/lib/active_support/core_ext/integer.rb b/activesupport/lib/active_support/core_ext/integer.rb index f5b185b42b..a44a1b4c74 100644 --- a/activesupport/lib/active_support/core_ext/integer.rb +++ b/activesupport/lib/active_support/core_ext/integer.rb @@ -1,4 +1,3 @@ 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 deleted file mode 100644 index 17a04d4d63..0000000000 --- a/activesupport/lib/active_support/core_ext/integer/inquiry.rb +++ /dev/null @@ -1,19 +0,0 @@ -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 negative. - # - # -1.negative? # => true - # 0.negative? # => false - # 1.negative? # => false - def negative? - self < 0 - end -end diff --git a/activesupport/lib/active_support/core_ext/numeric.rb b/activesupport/lib/active_support/core_ext/numeric.rb index a6bc0624be..bcdc3eace2 100644 --- a/activesupport/lib/active_support/core_ext/numeric.rb +++ b/activesupport/lib/active_support/core_ext/numeric.rb @@ -1,3 +1,4 @@ require 'active_support/core_ext/numeric/bytes' require 'active_support/core_ext/numeric/time' +require 'active_support/core_ext/numeric/inquiry' require 'active_support/core_ext/numeric/conversions' diff --git a/activesupport/lib/active_support/core_ext/numeric/inquiry.rb b/activesupport/lib/active_support/core_ext/numeric/inquiry.rb new file mode 100644 index 0000000000..b1a27dd698 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/numeric/inquiry.rb @@ -0,0 +1,19 @@ +class Numeric + # 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 negative. + # + # -1.negative? # => true + # 0.negative? # => false + # 1.negative? # => 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 6eeadb2ace..41736fb672 100644 --- a/activesupport/test/core_ext/integer_ext_test.rb +++ b/activesupport/test/core_ext/integer_ext_test.rb @@ -27,16 +27,4 @@ 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 diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index b82448458d..192b2ba577 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -389,4 +389,16 @@ class NumericExtFormattingTest < ActiveSupport::TestCase def test_in_milliseconds assert_equal 10_000, 10.seconds.in_milliseconds 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 -- cgit v1.2.3