Like Hash#reject but recursively.
Deletes recursively every key-value pair from hash for which block evaluates to true.
Add this line to your application's Gemfile:
gem 'hash_reject_recursively'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hash_reject_recursively
languages = {
ruby: { lines: 100 },
python: { lines: nil },
java: { lines: nil },
php: nil
}
new_hash = languages.deep_reject { |key, value| value.nil? }
# new_hash == { ruby: { lines: 100 }, python: {}, java: {} }
new_hash = languages.deep_reject { |key, value| value.blank? } # Rails example
# new_hash == { ruby: { lines: 100 } }
languages.deep_reject! { |key, value| value.nil? }
# languages == { ruby: { lines: 100 }, python: {}, java: {} }
Note 1: deep_reject!
changes the original object
Note 2: .blank?
in example above it's Rails implementation method.
The gem is available as open source under the terms of the MIT License.
First of all, thank you for wanting to help!
- Fork it.
- Create a feature branch -
git checkout -b more_magic
- Add tests and make your changes
- Check if tests are ok -
rake spec
- Commit changes -
git commit -am "Added more magic"
- Push to Github -
git push origin more_magic
- Send a pull request! ❤️