Class: Hash
- 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
Instance Method Summary collapse
- #bury(*args, &block) ⇒ Object
-
#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`).
- #str_keys(*args, &block) ⇒ Object
- #str_keys!(*args, &block) ⇒ Object
- #sym_keys(*args, &block) ⇒ Object
-
#sym_keys!(*args, &block) ⇒ Object
Short names.
- #to_options(*args, &block) ⇒ Object
- #to_options!(*args, &block) ⇒ Object
-
#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(*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
See NRSER.bury!
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.
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, █ stringify_keys *args, █ end |
#str_keys!(*args, &block) ⇒ Object
20 |
# File 'lib/nrser/core_ext/hash.rb', line 20 def str_keys! *args, █ stringify_keys! *args, █ end |
#sym_keys(*args, &block) ⇒ Object
18 |
# File 'lib/nrser/core_ext/hash.rb', line 18 def sym_keys *args, █ symbolize_keys *args, █ 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, █ symbolize_keys! *args, █ end |
#to_options(*args, &block) ⇒ Object
24 |
# File 'lib/nrser/core_ext/hash.rb', line 24 def *args, █ symbolize_keys *args, █ end |
#to_options!(*args, &block) ⇒ Object
23 |
# File 'lib/nrser/core_ext/hash.rb', line 23 def *args, █ symbolize_keys! *args, █ 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.
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 |