Class: Hash
- Includes:
- NRSER::Ext::Tree
- Defined in:
- lib/nrser/core_ext/hash.rb,
lib/nrser/core_ext/hash/extract_values_at.rb,
lib/nrser/core_ext/hash/transform_values_with_keys.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#bury!(key_path, value, parsed_key_type: :guess, clobber: false) ⇒ Object
See NRSER.bury!.
-
#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`).
-
#to_pair ⇒ Array
Checks that ‘self` contains a single key/value pair (`#length` of 1) and returns it as an array of length 2.
-
#transform_values_with_keys(&block) ⇒ Object
Just like #transform_values but yields ‘key, value`.
-
#transform_values_with_keys!(&block) ⇒ Object
Just like #transform_values_with_keys but mutates ‘self`.
Methods included from NRSER::Ext::Tree
#each_branch, #leaves, #map_branches, #map_leaves, #map_tree
Instance Method Details
#bury!(key_path, value, parsed_key_type: :guess, clobber: false) ⇒ Object
See NRSER.bury!
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nrser/core_ext/hash.rb', line 20 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.
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 |
#to_pair ⇒ Array
Checks that ‘self` contains a single key/value pair (`#length` of 1) and returns it as an array of length 2.
41 42 43 44 45 46 47 48 |
# File 'lib/nrser/core_ext/hash.rb', line 41 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 |