diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/README.rdoc | 2 | ||||
-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/lib/active_support/json/decoding.rb | 10 | ||||
-rw-r--r-- | activesupport/test/core_ext/integer_ext_test.rb | 12 |
6 files changed, 40 insertions, 9 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/README.rdoc b/activesupport/README.rdoc index a6424a353a..cd72f53821 100644 --- a/activesupport/README.rdoc +++ b/activesupport/README.rdoc @@ -10,7 +10,7 @@ outside of Rails. The latest version of Active Support can be installed with RubyGems: - % [sudo] gem install activesupport + % gem install activesupport Source code can be downloaded as part of the Rails project on GitHub: 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..17a04d4d63 --- /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 negative. + # + # -1.negative? # => true + # 0.negative? # => false + # 1.negative? # => false + def negative? + self < 0 + end +end diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 35548f3f56..2932954f03 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -9,20 +9,14 @@ module ActiveSupport module JSON # matches YAML-formatted dates DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?))$/ - + class << self # Parses a JSON string (JavaScript Object Notation) into a hash. # See http://www.json.org for more info. # # ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}") # => {"team" => "rails", "players" => "36"} - def decode(json, options = {}) - if options.present? - raise ArgumentError, "In Rails 4.1, ActiveSupport::JSON.decode no longer " \ - "accepts an options hash for MultiJSON. MultiJSON reached its end of life " \ - "and has been removed." - end - + def decode(json) data = ::JSON.parse(json, quirks_mode: true) if ActiveSupport.parse_json_times 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 |