Skip to content

rafaelbiriba/hash_reject_recursively

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hash Reject Recursively

Gem Version Code Climate Coverage Status Travis

Like Hash#reject but recursively.

Deletes recursively every key-value pair from hash for which block evaluates to true.

Installation

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

Usage

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.

License

The gem is available as open source under the terms of the MIT License.

Contributing

First of all, thank you for wanting to help!

  1. Fork it.
  2. Create a feature branch - git checkout -b more_magic
  3. Add tests and make your changes
  4. Check if tests are ok - rake spec
  5. Commit changes - git commit -am "Added more magic"
  6. Push to Github - git push origin more_magic
  7. Send a pull request! ❤️

About

(Like Hash#reject but recursively). Deletes recursively every key-value pair from hash for which block evaluates to true.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages