diff options
author | tinogomes <tinorj@gmail.com> | 2014-01-08 15:46:42 -0200 |
---|---|---|
committer | tinogomes <tinorj@gmail.com> | 2014-01-09 16:02:55 -0200 |
commit | 51215937874dd5e8705701c772b41a7ce522f67c (patch) | |
tree | 840450f8e54d8c98e4ef2db8c7d176674a547330 | |
parent | f4fc9e65ed4d51c50ff117ea337206a11b463eeb (diff) | |
download | rails-51215937874dd5e8705701c772b41a7ce522f67c.tar.gz rails-51215937874dd5e8705701c772b41a7ce522f67c.tar.bz2 rails-51215937874dd5e8705701c772b41a7ce522f67c.zip |
Adding Hash#compact and Hash#compact! methods
* Adding Hash#compact and Hash#compact! methods
* Using Ruby 1.9 syntax on documentation
* Updating guides for `Hash#compact` and `Hash#compact!` methods
* Updating CHANGELOG for ActiveSupport
* Removing unecessary protected method and lambda for `Hash#compact` implementations
* Performing `Hash#compact` implementation - https://gist.github.com/tinogomes/8332883
* fixing order position
* Fixing typo
-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` ---------------------- |