1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Document best-practices for writing hash methods (#5805)

* Discussion is as per https://bugs.ruby-lang.org/issues/18611.

Co-authored-by: Sam Bostock <sam.bostock@shopify.com>
This commit is contained in:
Chris Seaton 2022-04-30 11:57:51 +01:00 committed by GitHub
parent 5c843a1a6e
commit 3a8d60f503
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2022-04-30 19:58:27 +09:00
Merged-By: eregon <eregontp@gmail.com>

13
hash.c
View file

@ -303,6 +303,19 @@ objid_hash(VALUE obj)
*
* Certain core classes such as Integer use built-in hash calculations and
* do not call the #hash method when used as a hash key.
*
* When implementing your own #hash based on multiple values, the best
* practice is to combine the class and any values using the hash code of an
* array:
*
* For example:
*
* def hash
* [self.class, a, b, c].hash
* end
*
* The reason for this is that the Array#hash method already has logic for
* safely and efficiently combining multiple hash values.
*--
* \private
*++