Class: Hash

Inherits:
Object show all
Includes:
NRSER::Ext::Tree
Defined in:
lib/nrser/core_ext/hash.rb,
lib/nrser/core_ext/hash/bury.rb,
lib/nrser/core_ext/hash/extract_values_at.rb,
lib/nrser/core_ext/hash/transform_values_with_keys.rb

Direct Known Subclasses

NRSER::RSpex::Opts, NRSER::Stash

Instance Method Summary collapse

Methods included from NRSER::Ext::Tree

#each_branch, #leaves, #map_branches, #map_leaves, #map_tree

Instance Method Details

#bury(*args, &block) ⇒ Object



20
21
22
# File 'lib/nrser/core_ext/hash/bury.rb', line 20

def bury *args, &block
  deep_dup.tap { |hash| hash.bury! *args, &block }
end

#bury!(key_path, value, parsed_key_type: :guess, clobber: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/nrser/core_ext/hash/bury.rb', line 9

def bury! key_path,
          value,
          parsed_key_type: :guess,
          clobber: false
  NRSER.bury! self,
              key_path,
              value,
              parsed_key_type: parsed_key_type,
              clobber: clobber
end

#extract_values_at!(*keys, into: []) ⇒ into

Like #extract! combined with #values_at - extracts ‘keys` and appends (via `#<<`) the values to `into` (in order of `keys`).

‘into` default to an empty Array.

Examples:

Basic Usage

hash = { a: 1, b: 2, c: 3, d: 4 }

hash.extract_values_at! :a, :b
# => [1, 2]

hash
# => {c: 3, d: 4}

hash = { a: 1, b: 2, c: 3, d: 4 }

hash.extract_values_at! :b, :a
# => [2, 1]

hash
# => {c: 3, d: 4}

Custom ‘into

hash = { a: 1, b: 2, c: 3, d: 4 }
into = Set[1, 3, 5]

hash.extract_values_at! :a, :b, into: into
# => #<Set: {1, 3, 5, 2}>

hash
# => {:c=>3, :d=>4}

Parameters:

  • keys

    Hash keys to extract.

  • into (#<<) (defaults to: [])

    Object to extract values at ‘keys` into.

Returns:

  • (into)

    The ‘into` object with the extracted values (if any are found).



43
44
45
46
47
# File 'lib/nrser/core_ext/hash/extract_values_at.rb', line 43

def extract_values_at! *keys, into: []
  keys.each_with_object( into ) { |key, result|
    result << delete(key) if has_key?( key )
  }
end

#str_keys(*args, &block) ⇒ Object



21
# File 'lib/nrser/core_ext/hash.rb', line 21

def str_keys  *args, &block;  stringify_keys  *args, &block;  end

#str_keys!(*args, &block) ⇒ Object



20
# File 'lib/nrser/core_ext/hash.rb', line 20

def str_keys! *args, &block;  stringify_keys! *args, &block;  end

#sym_keys(*args, &block) ⇒ Object



18
# File 'lib/nrser/core_ext/hash.rb', line 18

def sym_keys  *args, &block;  symbolize_keys  *args, &block;  end

#sym_keys!(*args, &block) ⇒ Object

Short names

NOTE If we use ‘alias_method` here it breaks subclasses that override

`#symbolize_keys`, etc. - like {HashWithIndifferentAccess}


17
# File 'lib/nrser/core_ext/hash.rb', line 17

def sym_keys! *args, &block;  symbolize_keys! *args, &block;  end

#to_options(*args, &block) ⇒ Object



24
# File 'lib/nrser/core_ext/hash.rb', line 24

def to_options  *args, &block;  symbolize_keys  *args, &block;  end

#to_options!(*args, &block) ⇒ Object



23
# File 'lib/nrser/core_ext/hash.rb', line 23

def to_options! *args, &block;  symbolize_keys! *args, &block;  end

#to_pairArray

Checks that ‘self` contains a single key/value pair (`#length` of 1) and returns it as an array of length 2.

Returns:

  • (Array)

    Array of length 2.

Raises:

  • (TypeError)

    If ‘self` has more than one key/value pair.



35
36
37
38
39
40
41
42
# File 'lib/nrser/core_ext/hash.rb', line 35

def to_pair
  unless length == 1
    raise TypeError,
          "Hash has more than one pair: #{ self.inspect }"
  end
  
  first
end

#transform_values_with_keys(&block) ⇒ Object

Just like #transform_values but yields ‘key, value`.



4
5
6
7
8
9
10
11
12
# File 'lib/nrser/core_ext/hash/transform_values_with_keys.rb', line 4

def transform_values_with_keys &block
  return enum_for( __method__ ) { size } unless block_given?
  return {} if empty?
  result = self.class.new
  each do |key, value|
    result[key] = block.call key, value
  end
  result
end

#transform_values_with_keys!(&block) ⇒ Object

Just like #transform_values_with_keys but mutates ‘self`.



17
18
19
20
21
22
# File 'lib/nrser/core_ext/hash/transform_values_with_keys.rb', line 17

def transform_values_with_keys! &block
  return enum_for( __method__ ) { size } unless block_given?
  each do |key, value|
    self[key] = block.call key, value
  end
end