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:
parent
5c843a1a6e
commit
3a8d60f503
Notes:
git
2022-04-30 19:58:27 +09:00
Merged-By: eregon <eregontp@gmail.com>
1 changed files with 13 additions and 0 deletions
13
hash.c
13
hash.c
|
@ -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
|
||||
*++
|
||||
|
|
Loading…
Add table
Reference in a new issue