# What is the Hash datatype in Ruby?

**URL:** <https://www.ruby-forum.com/t/what-is-the-hash-datatype-in-ruby/172947>\
**Category:** Ruby\
**Created:** [July 30, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/what-is-the-hash-datatype-in-ruby/172947 "2009-07-30T13:44:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 30, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/what-is-the-hash-datatype-in-ruby/172947/1 "2009-07-30T13:44:01Z")

</div>

What is the Hash datatype in Ruby? And what is it used for?

Can anyone please explain with an example.

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 30, 2009, 2:03pm UTC](https://www.ruby-forum.com/t/what-is-the-hash-datatype-in-ruby/172947/2 "2009-07-30T14:03:07Z")

</div>

On 30.07.2009 13:44, Prateek A. wrote:

> What is the Hash datatype in Ruby? And what is it used for?
> 
> Can anyone please explain with an example.

[http://www.ruby-doc.org/core/classes/Hash.html](http://www.ruby-doc.org/core/classes/Hash.html)

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 30, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/what-is-the-hash-datatype-in-ruby/172947/3 "2009-07-30T14:20:05Z")

</div>

A hash is a collection of pieces of data where you access any selected  
item via a key rather than by position (as with eg arrays and lists).

Whether Ruby does this I don’t know but in the olden days hash implied  
mapping a key into an address space using a randomizing algorithm which  
as far as possible kept items at different addresses. If the hash  
created a collision then the item would be moved to some predictable  
offset position. It’s therefore a sort of indexing mechanism that  
doesn’t require an index.

Arguably it makes sense to use a hash whenever you are dealing with  
collections that are not accessed in a particular order.

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 30, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/what-is-the-hash-datatype-in-ruby/172947/4 "2009-07-30T14:28:52Z")

</div>

Am Donnerstag, 30. Jul 2009, 20:44:05 +0900 schrieb Prateek A.:

> What is the Hash datatype in Ruby? And what is it used for?
> 
> Can anyone please explain with an example.

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut\_containers.html#UB](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html#UB)
