Skip to content

Commit 42b75a9

Browse files
[DOC] Tweaks for Hash#merge (#12825)
1 parent f118e0c commit 42b75a9

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

hash.c

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,53 +4117,48 @@ rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
41174117

41184118
/*
41194119
* call-seq:
4120-
* merge -> copy_of_self
41214120
* merge(*other_hashes) -> new_hash
41224121
* merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
41234122
*
4124-
* Returns the new +Hash+ formed by merging each of +other_hashes+
4125-
* into a copy of +self+.
4123+
* Each argument +other_hash+ in +other_hashes+ must be a hash.
41264124
*
4127-
* Each argument in +other_hashes+ must be a +Hash+.
4125+
* With arguments +other_hashes+ given and no block,
4126+
* returns the new hash formed by merging each successive +other_hash+
4127+
* into a copy of +self+;
4128+
* returns that copy;
4129+
* for each successive entry in +other_hash+:
41284130
*
4129-
* ---
4130-
*
4131-
* With arguments and no block:
4132-
* * Returns the new +Hash+ object formed by merging each successive
4133-
* +Hash+ in +other_hashes+ into +self+.
4134-
* * Each new-key entry is added at the end.
4135-
* * Each duplicate-key entry's value overwrites the previous value.
4131+
* - For a new key, the entry is added at the end of +self+.
4132+
* - For duplicate key, the entry overwrites the entry in +self+,
4133+
* whose position is unchanged.
41364134
*
41374135
* Example:
4136+
*
41384137
* h = {foo: 0, bar: 1, baz: 2}
41394138
* h1 = {bat: 3, bar: 4}
41404139
* h2 = {bam: 5, bat:6}
41414140
* h.merge(h1, h2) # => {foo: 0, bar: 4, baz: 2, bat: 6, bam: 5}
41424141
*
4143-
* With arguments and a block:
4144-
* * Returns a new +Hash+ object that is the merge of +self+ and each given hash.
4145-
* * The given hashes are merged left to right.
4146-
* * Each new-key entry is added at the end.
4147-
* * For each duplicate key:
4148-
* * Calls the block with the key and the old and new values.
4149-
* * The block's return value becomes the new value for the entry.
4142+
* With arguments +other_hashes+ and a block given, behaves as above
4143+
* except that for a duplicate key
4144+
* the overwriting entry takes it value not from the entry in +other_hash+,
4145+
* but instead from the block:
4146+
*
4147+
* - The block is called with the duplicate key and the values
4148+
* from both +self+ and +other_hash+.
4149+
* - The block's return value becomes the new value for the entry in +self+.
41504150
*
41514151
* Example:
4152+
*
41524153
* h = {foo: 0, bar: 1, baz: 2}
41534154
* h1 = {bat: 3, bar: 4}
41544155
* h2 = {bam: 5, bat:6}
4155-
* h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
4156-
* h3 # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
4156+
* h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
4157+
* # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
41574158
*
4158-
* With no arguments:
4159-
* * Returns a copy of +self+.
4160-
* * The block, if given, is ignored.
4159+
* With no arguments, returns a copy of +self+; the block, if given, is ignored.
41614160
*
4162-
* Example:
4163-
* h = {foo: 0, bar: 1, baz: 2}
4164-
* h.merge # => {foo: 0, bar: 1, baz: 2}
4165-
* h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' }
4166-
* h1 # => {foo: 0, bar: 1, baz: 2}
4161+
* Related: see {Methods for Assigning}[rdoc-ref:Hash@Methods+for+Assigning].
41674162
*/
41684163

41694164
static VALUE

0 commit comments

Comments
 (0)