aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-10-24 23:56:09 -0200
committerGitHub <noreply@github.com>2016-10-24 23:56:09 -0200
commit3cc30db12ca4b92d6c46a2fd28b2622c7ba4fe42 (patch)
tree56033f8c3b5e08664728ddc220a3654797fd18bf /activesupport
parentaad14c92990551cdd4d0d536b1944b17098480c7 (diff)
parent69a3fa1efc979f9d65110560d9779c2f8a74d8f6 (diff)
downloadrails-3cc30db12ca4b92d6c46a2fd28b2622c7ba4fe42.tar.gz
rails-3cc30db12ca4b92d6c46a2fd28b2622c7ba4fe42.tar.bz2
rails-3cc30db12ca4b92d6c46a2fd28b2622c7ba4fe42.zip
Merge pull request #26868 from prathamesh-sonpatki/use-hash-compact-from-ruby-24
Use Hash#compact and Hash#compact! from Ruby 2.4
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md9
-rw-r--r--activesupport/lib/active_support/core_ext/hash/compact.rb40
2 files changed, 29 insertions, 20 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 13d152f4c6..83ff80e31a 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Use `Hash#compact` and `Hash#compact!` from Ruby 2.4. Old Ruby versions
+ will continue to get these methods from Active Support as before.
+
+ *Prathamesh Sonpatki*
+
* Fix `ActiveSupport::TimeZone#strptime`.
Support for timestamps in format of seconds (%s) and milliseconds (%Q).
@@ -21,10 +26,10 @@
Time.zone = "US/Eastern"
t = Time.zone.local(2016,11,6,1)
- # => Sun, 06 Nov 2016 01:00:00 EDT -05:00
+ # => Sun, 06 Nov 2016 01:00:00 EDT -05:00
t.in(1.hour)
- # => Sun, 06 Nov 2016 01:00:00 EST -05:00
+ # => Sun, 06 Nov 2016 01:00:00 EST -05:00
Fixes #26580.
diff --git a/activesupport/lib/active_support/core_ext/hash/compact.rb b/activesupport/lib/active_support/core_ext/hash/compact.rb
index 78b3387c3b..5cae495bda 100644
--- a/activesupport/lib/active_support/core_ext/hash/compact.rb
+++ b/activesupport/lib/active_support/core_ext/hash/compact.rb
@@ -1,23 +1,27 @@
class Hash
- # Returns a hash with non +nil+ values.
- #
- # hash = { a: true, b: false, c: nil }
- # hash.compact # => { a: true, b: false }
- # hash # => { a: true, b: false, c: nil }
- # { c: nil }.compact # => {}
- # { c: true }.compact # => { c: true }
- def compact
- select { |_, value| !value.nil? }
+ unless Hash.instance_methods(false).include?(:compact)
+ # Returns a hash with non +nil+ values.
+ #
+ # hash = { a: true, b: false, c: nil }
+ # hash.compact # => { a: true, b: false }
+ # hash # => { a: true, b: false, c: nil }
+ # { c: nil }.compact # => {}
+ # { c: true }.compact # => { c: true }
+ def compact
+ select { |_, value| !value.nil? }
+ end
end
- # Replaces current hash with non +nil+ values.
- # Returns nil if no changes were made, otherwise returns the hash.
- #
- # hash = { a: true, b: false, c: nil }
- # hash.compact! # => { a: true, b: false }
- # hash # => { a: true, b: false }
- # { c: true }.compact! # => nil
- def compact!
- reject! { |_, value| value.nil? }
+ unless Hash.instance_methods(false).include?(:compact!)
+ # Replaces current hash with non +nil+ values.
+ # Returns nil if no changes were made, otherwise returns the hash.
+ #
+ # hash = { a: true, b: false, c: nil }
+ # hash.compact! # => { a: true, b: false }
+ # hash # => { a: true, b: false }
+ # { c: true }.compact! # => nil
+ def compact!
+ reject! { |_, value| value.nil? }
+ end
end
end