Ruby使用了硬方法ex39.hash函数和%运算符

Ruby使用了硬方法ex39.hash函数和%运算符,ruby,Ruby,我是Ruby新手,通过学习Ruby the Hard Way online book学习Ruby。在Ex39中我不是很清楚: 混淆代码: def Dict.hash_key(aDict, key) # Given a key this will create a number and then convert it to # an index for the aDict's buckets. #takes the key arguments and converts it into a has

我是Ruby新手,通过学习Ruby the Hard Way online book学习Ruby。在Ex39中我不是很清楚:

混淆代码:

def Dict.hash_key(aDict, key)
# Given a key this will create a number and then convert it to
# an index for the aDict's buckets.

#takes the key arguments and converts it into a hash number 
# that is divided by the length
#of the Dict. The remainder a number on the aDict.
return key.hash % aDict.length
end

%运算符的用途是什么。如果key.has创建了一个唯一的数字,为什么我们需要将它除以Dict的长度?

则%运算符是模运算符。(与当前大多数其他语言一样) 使用它,您可以确保最终的数字永远不会大于您所除以的值