Skip to content

Commit

Permalink
Adding Hash#compact and Hash#compact! methods
Browse files Browse the repository at this point in the history
  * 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
  • Loading branch information
tinogomes committed Jan 9, 2014
1 parent f4fc9e6 commit 5121593
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -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 * Maintain proleptic gregorian in Time#advance


`Time#advance` uses `Time#to_date` and `Date#advance` to calculate a new date. `Time#advance` uses `Time#to_date` and `Date#advance` to calculate a new date.
Expand Down
1 change: 1 addition & 0 deletions activesupport/lib/active_support/core_ext/hash.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_support/core_ext/hash/compact'
require 'active_support/core_ext/hash/conversions' require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/except'
Expand Down
20 changes: 20 additions & 0 deletions activesupport/lib/active_support/core_ext/hash/compact.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def test_methods
assert_respond_to h, :deep_stringify_keys! assert_respond_to h, :deep_stringify_keys!
assert_respond_to h, :to_options assert_respond_to h, :to_options
assert_respond_to h, :to_options! assert_respond_to h, :to_options!
assert_respond_to h, :compact
assert_respond_to h, :compact!
end end


def test_transform_keys def test_transform_keys
Expand Down Expand Up @@ -865,6 +867,32 @@ def test_except_with_mocha_expectation_on_original
original.expects(:delete).never original.expects(:delete).never
original.except(:a) original.except(:a)
end 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 end


class IWriteMyOwnXML class IWriteMyOwnXML
Expand Down
10 changes: 10 additions & 0 deletions guides/source/active_support_core_extensions.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2907,6 +2907,16 @@ The method `with_indifferent_access` returns an `ActiveSupport::HashWithIndiffer


NOTE: Defined in `active_support/core_ext/hash/indifferent_access.rb`. 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` Extensions to `Regexp`
---------------------- ----------------------


Expand Down

0 comments on commit 5121593

Please sign in to comment.