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

Instance Method Details

#partition_by_keys(*ks) ⇒ Hash

Returns Partitioned results based on whether keys are included within the ks argument.

Examples:

{a: 1, b: 2, c: 3}.partition_by_keys :a, :b # returns [{a: 1, b: 2}, {c: 3}]

Parameters:

  • ks (Glob of any type)

Returns:

  • (Hash)

    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.

Examples:

{a: 1, b: 2, c: 3}.partition_by_values 2, 3 # returns [{b: 2, c: 3}, {a: 1}]

Parameters:

  • vs (Glob of any type)

Returns:

  • (Hash)

    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.

Examples:

{a: 1, b: 2, c: 3}.reject_by_keys :a, :b # returns {c: 3}

Parameters:

  • ks (Glob of any type)

Returns:

  • (Hash)

    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.

Examples:

{a: 1, b: 2, c: 3}.reject_by_values 2, 3 # returns {a: 1}

Parameters:

  • vs (Glob of any type)

Returns:

  • (Hash)

    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.

Examples:

{a: 1, b: 2, c: 3}.select_by_keys :a, :b # returns {a: 1, b: 2}

Parameters:

  • ks (Glob of any type)

Returns:

  • (Hash)

    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.

Examples:

{a: 1, b: 2, c: 3}.select_by_values 2, 3 # returns {b: 2, c: 3}

Parameters:

  • vs (Glob of any type)

Returns:

  • (Hash)

    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