DEV Community

Edouble79
Edouble79

Posted on

Blog Post 13 Understanding Strings, Arrays, and Hash in Ruby

Confused manAs I continued to take a step back to understand Ruby more I came across Strings, Arrays, and Hash. Sometimes I feel I have to simplify the terminology for me to understand and grasp the programming language better.

To me the term String refers to basic piece of data. It may contain letters, numbers, or even symbols. An example of this can be, "I love Ruby" or 'I love Ruby'. Strings can be done by using single or double quotes as mentioned earlier.

What is an Array? It is like a container or even structure that can store multiple values. What does that even mean? Here is an example that I used using my family members.

family = Array["Esme", "Aria", "Crystal"]

The container/structure in that code above is family and the names of my family members are the array elements.

Lastly a Hash is like a collection of unique things to a specific value. What does that even mean right? It almost acts like an array where we store pieces of information but with a Hash you can store something called key value pair. We can store a value and give it a key which is kind of like giving it a name. For instance, using a dictionary we have a word and then the definition. So the word is the "key" and the "value" would be the definition.

At first I did not understand it so I had to read and watch videos to dumb it down for me. Here is an example that helped me understand it.

states = {
"Illinois" => "IL",
"Puerto Rico" => "SJ"
}

Here we have the states in "key" form and the value would be the abbreviated states in two letters. The => symbols serve as a "map" to link the "key"/state in to the "value"/letters on the right. Seeing this multiple times has helped me understand how it works. Now applying it in a real job setting....only time will tell.

Top comments (0)