aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/hash/compact.rb
diff options
context:
space:
mode:
authorPrathamesh Sonpatki <csonpatki@gmail.com>2016-10-23 20:03:40 +0530
committerPrathamesh Sonpatki <csonpatki@gmail.com>2016-10-23 20:54:39 +0530
commit69a3fa1efc979f9d65110560d9779c2f8a74d8f6 (patch)
tree7403e5a835d5b1cf061aa22e88a2a67a7a2f24bd /activesupport/lib/active_support/core_ext/hash/compact.rb
parent8447f8d01c3722593ccd345fd7e88be8039ecbf1 (diff)
downloadrails-69a3fa1efc979f9d65110560d9779c2f8a74d8f6.tar.gz
rails-69a3fa1efc979f9d65110560d9779c2f8a74d8f6.tar.bz2
rails-69a3fa1efc979f9d65110560d9779c2f8a74d8f6.zip
Use Hash#compact and Hash#compact! from Ruby 2.4
- Ruby 2.4 has added Hash#compact and Hash#compact! so we can use it now. - Reference: https://bugs.ruby-lang.org/issues/11818 and https://bugs.ruby-lang.org/issues/12863.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/compact.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/compact.rb40
1 files changed, 22 insertions, 18 deletions
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