What is the Hash datatype in Ruby?

P

Prateek Agarwal

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

Can anyone please explain with an example.
 
M

Mike Stephens

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top