diff options
author | Chris Seaton <chris@chrisseaton.com> | 2022-04-30 11:57:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-30 12:57:51 +0200 |
commit | 3a8d60f50388016a677f250eb4d7c8bbe9d75d7d (patch) | |
tree | 748e54f807a84160e38d77ecdfeb874224c14e79 | |
parent | 5c843a1a6e24aeabb3497065a362caf7b3e2d3b1 (diff) |
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>
Notes
Notes:
Merged-By: eregon <eregontp@gmail.com>
-rw-r--r-- | hash.c | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -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 *++ |