diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-09 10:14:25 -0800 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-01-09 10:14:25 -0800 |
commit | a67f25d50b5dd9585bb10ecd19bee6a34d69de6a (patch) | |
tree | 7905eb28c88cd6eb900c59beb23c12166bf868dd | |
parent | 4c32b729517a9a94e3d2a43f76df864fc5fc6e77 (diff) | |
parent | 51215937874dd5e8705701c772b41a7ce522f67c (diff) | |
download | rails-a67f25d50b5dd9585bb10ecd19bee6a34d69de6a.tar.gz rails-a67f25d50b5dd9585bb10ecd19bee6a34d69de6a.tar.bz2 rails-a67f25d50b5dd9585bb10ecd19bee6a34d69de6a.zip |
Merge pull request #13632 from tinogomes/master
Adding Hash#compact and Hash#compact! methods
-rw-r--r-- | activesupport/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/compact.rb | 20 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 28 | ||||
-rw-r--r-- | guides/source/active_support_core_extensions.md | 10 |
5 files changed, 63 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 2cb1e79365..adaeb99c87 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Added `Hash#compact` and `Hash#compact!` for removing items with nil value from hash. + + *Celestino Gomes* + * Maintain proleptic gregorian in Time#advance `Time#advance` uses `Time#to_date` and `Date#advance` to calculate a new date. diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb index 686f12c6da..f68e1662f9 100644 --- a/activesupport/lib/active_support/core_ext/hash.rb +++ b/activesupport/lib/active_support/core_ext/hash.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/hash/compact' require 'active_support/core_ext/hash/conversions' require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/hash/except' diff --git a/activesupport/lib/active_support/core_ext/hash/compact.rb b/activesupport/lib/active_support/core_ext/hash/compact.rb new file mode 100644 index 0000000000..6566215a4d --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/compact.rb @@ -0,0 +1,20 @@ +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 # => {} + def compact + self.select { |_, value| !value.nil? } + end + + # Replaces current hash with non +nil+ values. + # + # hash = { a: true, b: false, c: nil} + # hash.compact! # => { a: true, b: false} + # hash # => { a: true, b: false} + def compact! + self.reject! { |_, value| value.nil? } + end +end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 1ee9fbf46e..40c8f03374 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -54,6 +54,8 @@ class HashExtTest < ActiveSupport::TestCase assert_respond_to h, :deep_stringify_keys! assert_respond_to h, :to_options assert_respond_to h, :to_options! + assert_respond_to h, :compact + assert_respond_to h, :compact! end def test_transform_keys @@ -865,6 +867,32 @@ class HashExtTest < ActiveSupport::TestCase original.expects(:delete).never original.except(:a) end + + def test_compact + hash_contain_nil_value = @symbols.merge(z: nil) + hash_with_only_nil_values = { a: nil, b: nil } + + h = hash_contain_nil_value.dup + assert_equal(@symbols, h.compact) + assert_equal(hash_contain_nil_value, h) + + h = hash_with_only_nil_values.dup + assert_equal({}, h.compact) + assert_equal(hash_with_only_nil_values, h) + end + + def test_compact! + hash_contain_nil_value = @symbols.merge(z: nil) + hash_with_only_nil_values = { a: nil, b: nil } + + h = hash_contain_nil_value.dup + assert_equal(@symbols, h.compact!) + assert_equal(@symbols, h) + + h = hash_with_only_nil_values.dup + assert_equal({}, h.compact!) + assert_equal({}, h) + end end class IWriteMyOwnXML diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index 1a43bd206e..59dfefd22f 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -2907,6 +2907,16 @@ The method `with_indifferent_access` returns an `ActiveSupport::HashWithIndiffer NOTE: Defined in `active_support/core_ext/hash/indifferent_access.rb`. +### Compacting + +The methods `compact` and `compact!` return a Hash without items with `nil` value. + +```ruby +{a: 1, b: 2, c: nil}.compact # => {a: 1, b: 2} +``` + +NOTE: Defined in `active_support/core_ext/hash/compact.rb`. + Extensions to `Regexp` ---------------------- |