Skip to content

Commit b3053cb

Browse files
[DOC] Tweaks for Object#hash
1 parent 6b197de commit b3053cb

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

hash.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -321,40 +321,35 @@ objid_hash(VALUE obj)
321321
#endif
322322
}
323323

324-
/**
324+
/*
325325
* call-seq:
326-
* obj.hash -> integer
327-
*
328-
* Generates an Integer hash value for this object. This function must have the
329-
* property that <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>.
330-
*
331-
* The hash value is used along with #eql? by the Hash class to determine if
332-
* two objects reference the same hash key. Any hash value that exceeds the
333-
* capacity of an Integer will be truncated before being used.
326+
* hash -> integer
334327
*
335-
* The hash value for an object may not be identical across invocations or
336-
* implementations of Ruby. If you need a stable identifier across Ruby
337-
* invocations and implementations you will need to generate one with a custom
338-
* method.
328+
* Returns the integer hash value for +self+;
329+
* has the property that if <tt>foo.eql?(bar)</tt>
330+
* then <tt>foo.hash == bar.hash</tt>.
339331
*
340-
* Certain core classes such as Integer use built-in hash calculations and
341-
* do not call the #hash method when used as a hash key.
332+
* \Class Hash uses both #hash and #eql? to determine whether two objects
333+
* used as hash keys are to be treated as the same key.
334+
* A hash value that exceeds the capacity of an Integer is truncated before being used.
342335
*
343-
* When implementing your own #hash based on multiple values, the best
344-
* practice is to combine the class and any values using the hash code of an
345-
* array:
336+
* Many core classes override method Object#hash;
337+
* other core classes (e.g., Integer) calculate the hash internally,
338+
* and do not call the #hash method when used as a hash key.
346339
*
347-
* For example:
340+
* When implementing #hash for a user-defined class,
341+
* best practice is to use Array#hash with the class name and the values
342+
* that are important in the instance;
343+
* this takes advantage of that method's logic for safely and efficiently
344+
* generating a hash value:
348345
*
349346
* def hash
350347
* [self.class, a, b, c].hash
351348
* end
352349
*
353-
* The reason for this is that the Array#hash method already has logic for
354-
* safely and efficiently combining multiple hash values.
355-
*--
356-
* \private
357-
*++
350+
* The hash value may differ among invocations or implementations of Ruby.
351+
* If you need stable hash-like identifiers across Ruby invocations and implementations,
352+
* use a custom method to generate them.
358353
*/
359354
VALUE
360355
rb_obj_hash(VALUE obj)

0 commit comments

Comments
 (0)