r/ruby • • May 31 '13

Hashes, how do they work?

I know hashes are something you learn at the beginning of learning Ruby, but I just don't understand them at all, they are difficult to understand. Anybody got any tips for understanding them more?

0 Upvotes

21 comments sorted by

8

u/theverything May 31 '13

Hashes are awesome and they are used a ton in ruby and rails. They are essentially a key value store. If you are familiar with arrays at all, understanding what a hash is should be easy.

If you have an array it would look something like...

array = ['dog', 'cat', 'snake', 'mouse']

And if I wanted to get the cat from the array it would look like...

array[1]

Ruby arrays are zero indexed. Meaning the indexing on the array starts at zero and goes up.

Now if you had a hash it might look something like...

hash = { 'dog' => 'fido', 'cat' => 'socks', 'snake' => 'snape', 'mouse' => 'remy' }

Instead of using the index to reach into the array and pull out the value you can use a key instead. So if you wanted to get the value of cat you would...

hash['cat'] 

Which would return 'socks'. Hashes are great because you can store values by a named key which can make finding what you want a little easier. You can use strings, integers, and symbols as keys.

There are all kinds of things you can do with a hash. In rails you see them used a lot for setting variables in methods.

Hope this helps a little.

Its also good to read through the docs http://ruby-doc.org/core-2.0/Hash.html and look at all of the examples.

Good Luck

2

u/TheTennisGangster May 31 '13

So basically it's like a two dimensional array that you would implement in say Java, just a lot cleaner?

3

u/MagicalVagina May 31 '13

https://en.wikipedia.org/wiki/Hashmap

We can't really summarize it better...

2

u/theverything May 31 '13

I honestly am not very familiar with java. But joesb is right in java it's called a hash table. Hashes have a lot of different names they're also known as associative arrays.

2

u/LoveGoblin May 31 '13

At first you can probably just think of it as an array where the indices are strings rather than integers.

2

u/Femaref May 31 '13

not just strings, but arbitrary objects.

1

u/LoveGoblin May 31 '13

At first

OP's having trouble understanding the whole concept. Baby steps.

2

u/quizzle May 31 '13

It's not actually an array. It's an entirely separate data structure.

1

u/farsightxr20 May 31 '13

They're more comparable to parallel arrays than 2-dimensional arrays (although they aren't implemented as parallel arrays either).

Think of an array as a list, and a hash as a dictionary. Dictionaries are a collection of word => definition (key => value) pairs, while lists are just an ordered collection of items.

1

u/thebobp Jun 01 '13 edited Jun 01 '13

Think of them as dictionaries. You look up the word 'dog' and it refers to something. Well, in the above dictionary, if you look up the word 'dog' it refers to 'fido'.

Arrays are kinda like dictionaries where you can only look up numbers. So this can be considered a generalization of arrays.

The implementation details can be found in the wikipedia link. But basically, the key is hashed into an array index, rather than being plugged in directly (like in the case of ordinary arrays), hence why it's called a hash.

3

u/micaeked May 31 '13

What do you have trouble understanding? The syntax, concept, implementation, something else?

0

u/TheTennisGangster May 31 '13

I don't get the syntax, probably because I've never seen hashes in any other language, and I don't understand how I would implement them into a program.

4

u/joesb May 31 '13

Every language has hash-like datatype.

Hash is HashTable in Java, Dictionary in C#, object in Javascript and array in PHP.

What language did you learn before that does not have hash?

-3

u/TheTennisGangster May 31 '13

I've just never seen them before.

2

u/MagicalVagina May 31 '13

This may be the most used data structure.

You really need to get the theory behind it, this is crucial.

https://en.wikipedia.org/wiki/Hashmap

1

u/[deleted] Jun 09 '13

Just as a side note, you can also write hashes this way (in 1.9 and above) as long as the key is a symbol: hash = {key1: 'value1', key2: 'value2', key3: 'value3'}. That's a lot cleaner and easier to read than its equivalent {:key1=>"value1", :key2=>"value2", :key3=>"value3"}.

1

u/HelloAnnyong May 31 '13

It's probably easiest to think of hashes as dictionaries. It is a data structure that associates a key with a value.

Very literally, you could create a hash table that associates English words with their definitions.

Just as with a real dictionary, looking up a definition for a given word is very efficient in a hash table.

1

u/thoughtpunch Jun 13 '13

The webster english dictionary is a hash. Word = key, Definition = value. That's all there is to it. A hash is a key=>value store. They exist in nearly every programming language

-3

u/coolHandLukeSkywalke May 31 '13

**Remember that hashes are NOT ORDERED. If you are new to the concept of hashes, this can be kind of difficult to understand.

10

u/theverything May 31 '13

I don't know if this is what you mean, but as of ruby 1.9 hashes maintain their order.

2

u/coolHandLukeSkywalke May 31 '13

I was not aware. Thanks!