在Ruby 中,有一个名为Hash
的内置类。 .根据the docs :
A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.
...
A Hash can be easily created by using its implicit form:
grades = { "Jane Doe" => 10, "Jim Doe" => 6 }
所以基本上,它们是 associative arrays .如果是这样,那么为什么将它们称为哈希?在计算机科学中,术语“散列”不是通常用来指代通过 hash function 运行某些数据生成的某种数字或某种十六进制字符串吗? ?事实上,Ruby 对象甚至有一个名为 hash
的方法。它“为此对象生成一个 Fixnum 哈希值”。
我知道 Hash
在 Ruby 中实现为 hash table ,但考虑到我上面所说的,似乎没有足够的理由在 Table
或 Map
之类的东西上使用名称 Hash
,特别是考虑到 Ruby 似乎没有任何其他内置的 associative arrays 实现.为什么选择这个名字?
最佳答案
Ruby 从 Perl 中汲取了大量灵感,Perl 将其称为散列。
更新:
由 Ruby 的创造者 Yukihiro Matsumoto 确认:https://twitter.com/yukihiro_matz/status/547516495249428480
关于ruby - 为什么 Ruby 哈希被称为哈希,而不是映射、字典、表或关联数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293147/