summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorChris Seaton <chris@chrisseaton.com>2022-04-30 11:57:51 +0100
committerGitHub <noreply@github.com>2022-04-30 12:57:51 +0200
commit3a8d60f50388016a677f250eb4d7c8bbe9d75d7d (patch)
tree748e54f807a84160e38d77ecdfeb874224c14e79 /hash.c
parent5c843a1a6e24aeabb3497065a362caf7b3e2d3b1 (diff)
downloadruby-3a8d60f50388016a677f250eb4d7c8bbe9d75d7d.tar.gz
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>
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index 1f2bca12b3..797fe6fbfe 100644
--- a/hash.c
+++ b/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
*++