Skip to content

Commit 0a10c9b

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Tweaks for Hash#compare_by_identity
1 parent a94f00f commit 0a10c9b

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

hash.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4401,28 +4401,26 @@ rb_hash_compact_bang(VALUE hash)
44014401
* call-seq:
44024402
* compare_by_identity -> self
44034403
*
4404-
* Sets +self+ to consider only identity in comparing keys;
4405-
* two keys are considered the same only if they are the same object;
4406-
* returns +self+.
4407-
*
4408-
* By default, these two object are considered to be the same key,
4409-
* so +s1+ will overwrite +s0+:
4410-
* s0 = 'x'
4411-
* s1 = 'x'
4404+
* Sets +self+ to compare keys using _identity_ (rather than mere _equality_);
4405+
* returns +self+:
4406+
*
4407+
* By default, two keys are considered to be the same key
4408+
* if and only if they are _equal_ objects (per method #==):
4409+
*
44124410
* h = {}
4413-
* h.compare_by_identity? # => false
4414-
* h[s0] = 0
4415-
* h[s1] = 1
4411+
* h['x'] = 0
4412+
* h['x'] = 1 # Overwrites.
44164413
* h # => {"x"=>1}
44174414
*
4418-
* After calling \#compare_by_identity, the keys are considered to be different,
4419-
* and therefore do not overwrite each other:
4420-
* h = {}
4421-
* h.compare_by_identity # => {}
4422-
* h.compare_by_identity? # => true
4423-
* h[s0] = 0
4424-
* h[s1] = 1
4425-
* h # => {"x"=>0, "x"=>1}
4415+
* When this method has been called, two keys are considered to be the same key
4416+
* if and only if they are the _same_ object:
4417+
*
4418+
* h.compare_by_identity
4419+
* h['x'] = 2 # Does not overwrite.
4420+
* h # => {"x"=>1, "x"=>2}
4421+
*
4422+
* Related: #compare_by_identity?;
4423+
* see also {Methods for Comparing}[rdoc-ref:Hash@Methods+for+Comparing].
44264424
*/
44274425

44284426
VALUE
@@ -4469,7 +4467,15 @@ rb_hash_compare_by_id(VALUE hash)
44694467
* call-seq:
44704468
* compare_by_identity? -> true or false
44714469
*
4472-
* Returns +true+ if #compare_by_identity has been called, +false+ otherwise.
4470+
* Returns whether #compare_by_identity has been called:
4471+
*
4472+
* h = {}
4473+
* h.compare_by_identity? # => false
4474+
* h.compare_by_identity
4475+
* h.compare_by_identity? # => true
4476+
*
4477+
* Related: #compare_by_identity;
4478+
* see also {Methods for Comparing}[rdoc-ref:Hash@Methods+for+Comparing].
44734479
*/
44744480

44754481
VALUE

0 commit comments

Comments
 (0)