Reputation: 48438
In Ruby, there is a built-in class called Hash
. According to 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 }
So basically, they're associative arrays. If that's the case, then why are they called hashes? In Computer Science, isn't the term "hash" usually used to refer to a number or hexadecimal string of some kind generated by running some data through a hash function? In fact, Ruby objects even have a method called hash
which "generates a Fixnum hash value for this object".
I'm aware that Hash
in Ruby is implemented as a hash table, but given what I've said above that doesn't seem like enough justification for using the name Hash
over something like Table
or Map
, especially given that Ruby doesn't seem to have any other built-in implementations of associative arrays. Why was this name chosen?
Upvotes: 8
Views: 862
Reputation: 20184
Ruby takes a large amount of inspiration from Perl, and Perl calls this a hash.
UPDATE:
Confirmed by Yukihiro Matsumoto, creator of Ruby: https://twitter.com/yukihiro_matz/status/547516495249428480
Upvotes: 6