merge
1.6 modified

merge(object) -> newHash

Merges object to hash and returns the result of that merge. Prior to v1.6.0: This was destructive (object's values were added to hash). Since v1.6.0: This is no longer destructive (hash is cloned before the operation).

Duplicate keys will cause an overwrite. This is useful for selectively overwriting values on specific keys (e.g. exerting some level of control over a series of options).

The argument can be a Hash or just a vanilla Object.

Examples

As of 1.6.0 Hash#merge returns a new hash:


var h = $H({ name: 'Prototype', version: 1.5 });

h.merge({ version: 1.6, author: 'Sam' }).inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.6, 'author': 'Sam'}>

h.inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.5}>

Prior to 1.6.0 Hash#merge was destructive:


var h = $H({ name: 'Prototype', version: 1.5 });

h.merge({ version: 1.6, author: 'Sam' }).inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.6, 'author': 'Sam'}>

h.inspect();
// -> #<Hash:{'name': 'Prototype', 'version': 1.6, 'author': 'Sam'}>

See also

If you are using 1.6.0 or above and need a destructive merge, try Hash#update.