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 /activesupport/lib/active_support/core_ext/hash/compact.rb | |
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
Diffstat (limited to 'activesupport/lib/active_support/core_ext/hash/compact.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/compact.rb | 20 |
1 files changed, 20 insertions, 0 deletions
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 |