Class: Hash
- Includes:
- Jinx::Hasher
- Defined in:
- lib/jinx/helpers/hash.rb,
lib/jinx/helpers/merge.rb
Direct Known Subclasses
Defined Under Namespace
Classes: EMPTY_HASH||=Hash.new
Instance Method Summary collapse
- #base__merge! ⇒ Object private
-
#merge(other, options = nil, &block) ⇒ Object
Returns a new hash which merges the other hash with this hash.
-
#merge!(other, options = nil, &block) ⇒ Object
Merges the other hash into this hash and returns this modified hash.
Methods included from Jinx::Hasher
#==, #[], #assoc_values, #compact, #compose, #copy_recursive, #detect_hash_value, #detect_key, #detect_key_with_value, #detect_value, #difference, #each_key, #each_pair, #each_value, #enum_keys, #enum_keys_with_value, #enum_values, #filter, #filter_on_key, #filter_on_value, #flatten, #has_key?, #has_value?, #inspect, #join, #keys, #pretty_print, #pretty_print_cycle, #qp, #reject_keys, #reject_values, #select_keys, #select_values, #sort, #split, #to_hash, #to_s, #to_set, #transform_key, #transform_value, #union, #values
Methods included from Enumerable
#enumerate, #pp_s, #pretty_print, #pretty_print_cycle, #qp, #to_enum, #transitive_closure
Methods included from Jinx::Collection
#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #flatten, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #to_series, #transform, #union
Instance Method Details
#base__merge! ⇒ Object (private)
22 |
# File 'lib/jinx/helpers/merge.rb', line 22 alias :base__merge! :merge! |
#merge(other, options = nil, &block) ⇒ Object
Returns a new hash which merges the other hash with this hash.
Supported options include the following:
-
:deep - merge values which match on the key.
If the :deep option is set, and a key matches both this hash and the other hash on hash values, then the other hash’s value is recursively merged into this Hash’s value using the non-destructive #merge method with the deep option set. If a block is given to this method, then the block is passed to the value merge.
18 19 20 |
# File 'lib/jinx/helpers/merge.rb', line 18 def merge(other, =nil, &block) dup.merge!(other, , &block) end |
#merge!(other, options = nil, &block) ⇒ Object
Merges the other hash into this hash and returns this modified hash.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jinx/helpers/merge.rb', line 28 def merge!(other, =nil, &block) # use the standard Hash merge unless the :deep option is set return base__merge!(other, &block) unless Options.get(:deep, ) # merge the other entries: # if the hash value is a hash, then call merge on that hash value. # otherwise, if the hash value understands merge, then call that method. # otherwise, if there is a block, then call the block. # otherwise, set the the hash value to the other value. base__merge!(other) do |key, oldval, newval| if Hash === oldval then oldval.merge(newval, , &block) elsif oldval.respond_to?(:merge) oldval.merge(newval, &block) elsif block_given? then yield(key, oldval, newval) else newval end end end |