Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Ruby hashes called hashes, and not maps, dicts, tables or associatve arrays?

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?

like image 341
Ajedi32 Avatar asked Jun 18 '14 18:06

Ajedi32


People also ask

Why do we use hashes in Ruby?

A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type. They are also called associative arrays, dictionaries or maps. If a hash is accessed with a key that does not exist, the method will return nil.

How does a Ruby hash object differ from an array?

Ruby's arrays and hashes are indexed collections. Both store collections of objects, accessible using a key. With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements.

Is associative array a hash map?

A dictionary (also known as a map, hashmap or associative array) is a set of key/value pairs. OpenAPI lets you define dictionaries where the keys are strings. To define a dictionary, use type: object and use the additionalProperties keyword to specify the type of values in key/value pairs.

What is hash map in Ruby?

A Map is a mapping of Distinct Keys to Values; there are Map variations which relax this, but Ruby's Hashmap follows the standard Map ADT. In this case an Array of two different Hashes (each with a "value" and a "details") is being created.


1 Answers

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

like image 59
nobody Avatar answered Sep 29 '22 07:09

nobody