diff options
author | Igor Pstyga <igor.pstyga@gmail.com> | 2016-06-03 04:54:34 -0700 |
---|---|---|
committer | Igor Pstyga <igor.pstyga@gmail.com> | 2016-06-03 04:54:34 -0700 |
commit | 0dd23446a32b4e4974572a39582ea4c11475b80a (patch) | |
tree | 10eac836cce15f96f0615c011dd6f6a8823173fe /activesupport/lib/active_support | |
parent | 082a5158251c6578714132e5c4f71bd39f462d71 (diff) | |
download | rails-0dd23446a32b4e4974572a39582ea4c11475b80a.tar.gz rails-0dd23446a32b4e4974572a39582ea4c11475b80a.tar.bz2 rails-0dd23446a32b4e4974572a39582ea4c11475b80a.zip |
Improve Hash#compact! documentation and tests
Make it clear what should be returned when no changes were made to the
hash.
{ c: true }.compact! # => nil
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/compact.rb | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/compact.rb b/activesupport/lib/active_support/core_ext/hash/compact.rb index 62ea579c65..f072530e04 100644 --- a/activesupport/lib/active_support/core_ext/hash/compact.rb +++ b/activesupport/lib/active_support/core_ext/hash/compact.rb @@ -2,18 +2,21 @@ 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 # => {} + # hash.compact # => { a: true, b: false } + # hash # => { a: true, b: false, c: nil } + # { c: nil }.compact # => {} + # { c: true }.compact # => { c: true } def compact self.select { |_, value| !value.nil? } 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 } + # hash.compact! # => { a: true, b: false } + # hash # => { a: true, b: false } + # { c: true }.compact! # => nil def compact! self.reject! { |_, value| value.nil? } end |