Hash operators
Hash operators accept hashes as their left operand, and hashes or specific kinds of arrays as their right operand. The expressions resolve to hash values.
+ (merging)
Resolves to a hash containing the keys and values in the left operand and the keys and values in the right operand. If a key is present in both operands, the final hash uses the value from the right. It does not merge hashes recursively; it only merges top-level keys.
The right operand can be one of the following:
A hash
An array with an even number of elements. Each pair is converted in order to a key-value hash pair.
{a => 10, b => 20} + {b => 30} # resolves to {a => 10, b => 30} {a => 10, b => 20} + {c => 30} # resolves to {a => 10, b => 30, c => 30} {a => 10, b => 20} + [c, 30] # resolves to {a => 10, b => 30, c => 30} {a => 10, b => 20} + 30 # gives an error {a => 10, b => 20} + [30] # gives an errorThe merging operator does not change its operands; it creates a new value.
- (removal)
Resolves to a hash containing the keys and values in the left operand, minus any keys that are also present in the right operand.
The right operand can be one of the following:
A hash. The keys present in this hash will be absent in the final hash, regardless of whether that key has the same values in both operands. The key, not the value, determines whether it's removed.
An array of keys.
A single key.
{a => first, b => second, c => 17} - {c => 17, a => "something else"} # resolves to {b => second} {a => first, b => second, c => 17} - {a => a, d => d} # resolves to {b => second, c => 17} {a => first, b => second, c => 17} - [c, a] # resolves to {b => second} {a => first, b => second, c => 17} - c # resolves to {a => first, b => second}The removal operator does not change its operands; it creates a new value.
Related information