I'm So Tired of Arrays
Same, that's why we have something different. Hash.
Did you just write what your cough sounds like?
No, I meant a Hash.
What's that?
It's a container, like Arrays, but it's different. The Hash, for python programmers is the Dictionary. A Hash is a key, value pair ordered list. In Ruby Hash keys are either symbols or strings(new Hash syntax turns them into symbols though). Here's an example:
myhash = {
:thing1 => 45,
:thing2 => 67,
:symbol1 => 67
}
In Ruby, Hash keys have to all be one of a kind inside of the specific Hash.
What about keys
:thing2
and:symbol1
?, they have the same value.
The value may look the same but to Ruby each key is pointing to different reserved memory for the integer, 67, so it's kind of like assigning two different variables.
Shorthand To Writing Hashes
If we're being honest doing this:
myhash = {:thing1 => 45, :thing2 => 67, :symbol1 => 67}
is weird, and takes up too much space. That's why Ruby developed a shorthand method to writing Hashes.
Here it is:
myhash = {thing1: 45, thing2: 67, symbol1: 67}
It's pretty much putting the colon for symbols at the back of the key's name. Much shorter right!
What if I want to make an empty Hash?
There are more than one way to do this.
myhash = Hash.new
or
myhash = {}
The outcome for both are the same, but what's actually happening is very different.
How?
In the first example myhash
is being assigned to Hash.new. What do you notice?
What's there to notice.
The .new method is used for creating new instances of a class. So, to break it down, you're assigning an instance of Ruby's builtin Hash class to the variable myhash
.
For the second example instead of creating a new instance of Hash you are assigning a Hash with nil in it to the variable myhash
.
How do I add an element to the Hash?
It acutally is a lot like adding to an Array. Here it is:
myhash = Hash.new
myhash[:key] = "value"
or
myhash = Hash.new
myhash = {key: "value"}
What's happening?
The first method is actually adding the key value pairs to the Hash. The second method is redefining the variable myhash
to have a key, value pair. The second method is redundant so I don't recommend using it.
Why Use A Hash?
It depends, but a Hash is very useful. Here's an example.
myarr = [["1", 3],["eleven", 5], ["3", 6]]
puts "#{myarr.to_h}"
The output will look like this
#=>{"1"=>3, "eleven"=>5, "3"=>6}
Why does that help?
Well, multi-dimensional Arrays are super confusing and at a point the become unsustainable. So by using the .to_h method we can turn the multidimensional Arrays into a Hash(the .to_h method is an Array method only, learn how to use it before you do!).
I see how the Hash is great for sorting data, but how do I use it?
It's simple, first though here's a Hash with a function as a value.
myhash = {"hi":56, "67":myfunc()}
To call myfunc
all we have to do is this:
myhash["67"]
Alright, Let's Use Hashes in Okay, We're Done!
We can keep going on and on about the diverse and complicated uses of a Hash, but that's not important. This is plenty for a beginner. I hope it wasn't too confusing, and that you learned something!
Top comments (2)
Well done, good intro!
As a nitpick, the shorthand section has a little mistake, as it should be:
that is equivalent to:
since the new hash syntax turns keys into symbols.
okay