Module: HashSelectors
- Defined in:
- lib/hash_selectors.rb
Overview
Provides additional select methods for Hashes. Automatically mixed-in to Hash on load.
Instance Method Summary collapse
-
#partition_by_keys(*ks) ⇒ Hash
Partitioned results based on whether keys are included within the ks argument.
-
#partition_by_values(*vs) ⇒ Hash
Partitioned results based on whether values are included within the vs argument.
-
#reject_by_keys(*ks) ⇒ Hash
That subset of the Hash whose keys are not found within the ks argument.
-
#reject_by_values(*vs) ⇒ Hash
That subset of the Hash whose values are not found within the vs argument.
-
#select_by_keys(*ks) ⇒ Hash
That subset of the Hash whose keys are included within the ks argument.
-
#select_by_values(*vs) ⇒ Hash
That subset of the Hash whose values are included within the vs argument.
Instance Method Details
#partition_by_keys(*ks) ⇒ Hash
Returns Partitioned results based on whether keys are included within the ks argument.
10 11 12 13 14 |
# File 'lib/hash_selectors.rb', line 10 def partition_by_keys(*ks) ### OPTIMIZE: Simple partition with the block results in nested Arrays. Investigate. blk = lambda { |k,v| ks.include?(k) } [select(&blk), reject(&blk)] end |
#partition_by_values(*vs) ⇒ Hash
Returns Partitioned results based on whether values are included within the vs argument.
21 22 23 24 25 |
# File 'lib/hash_selectors.rb', line 21 def partition_by_values(*vs) ### OPTIMIZE: Simple partition with the block results in nested Arrays. Investigate. blk = lambda { |k,v| vs.include?(v) } [select(&blk), reject(&blk)] end |
#reject_by_keys(*ks) ⇒ Hash
Returns That subset of the Hash whose keys are not found within the ks argument.
32 33 34 |
# File 'lib/hash_selectors.rb', line 32 def reject_by_keys(*ks) reject { |k,v| ks.include?(k) } end |
#reject_by_values(*vs) ⇒ Hash
Returns That subset of the Hash whose values are not found within the vs argument.
41 42 43 |
# File 'lib/hash_selectors.rb', line 41 def reject_by_values(*vs) reject { |k,v| vs.include?(v) } end |
#select_by_keys(*ks) ⇒ Hash
Returns That subset of the Hash whose keys are included within the ks argument.
50 51 52 |
# File 'lib/hash_selectors.rb', line 50 def select_by_keys(*ks) select { |k,v| ks.include?(k) } end |
#select_by_values(*vs) ⇒ Hash
Returns That subset of the Hash whose values are included within the vs argument.
59 60 61 |
# File 'lib/hash_selectors.rb', line 59 def select_by_values(*vs) select { |k,v| vs.include?(v) } end |