class Hash

Public Instance Methods

bury(*args, &block) click to toggle source
# 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) click to toggle source

See {NRSER.bury!}

# 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: []) click to toggle source

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

`into` default to an empty Array.

@example 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}

@example 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}

@param keys

Hash keys to extract.

@param [#<<] into

Object to extract values at `keys` into.

@return [into]

The `into` object with the extracted values (if any are found).
# 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;) click to toggle source
# File lib/nrser/core_ext/hash.rb, line 21
def str_keys  *args, &block;  stringify_keys  *args, &block;  end
str_keys!(*args, &block;) click to toggle source
# File lib/nrser/core_ext/hash.rb, line 20
def str_keys! *args, &block;  stringify_keys! *args, &block;  end
sym_keys(*args, &block;) click to toggle source
# File lib/nrser/core_ext/hash.rb, line 18
def sym_keys  *args, &block;  symbolize_keys  *args, &block;  end
sym_keys!(*args, &block;) click to toggle source

Short names

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

`#symbolize_keys`, etc. - like {HashWithIndifferentAccess}
# File lib/nrser/core_ext/hash.rb, line 17
def sym_keys! *args, &block;  symbolize_keys! *args, &block;  end
to_options(*args, &block;) click to toggle source
# File lib/nrser/core_ext/hash.rb, line 24
def to_options  *args, &block;  symbolize_keys  *args, &block;  end
to_options!(*args, &block;) click to toggle source
# File lib/nrser/core_ext/hash.rb, line 23
def to_options! *args, &block;  symbolize_keys! *args, &block;  end
to_pair() click to toggle source

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

@return [Array]

Array of length 2

@raise [TypeError]

If `self` has more than one key/value pair.
# 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) click to toggle source

Just like {Hash#transform_values} but yields `key, value`.

# 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) click to toggle source

Just like {#transform_values_with_keys} but mutates `self`.

# 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