Ruby 101: Data Structures

TK
The Renaissance Developer
4 min readApr 17, 2017

Part II: Array, Hash & Iteration through Data Structures

It is the second part of Beautiful Ruby series by Tk. If you are a totally beginner developer learning Ruby, you should start with part I of this series. The first part we learn about the history about Ruby & very basic stuff like variables, control flow (if-else statements) and looping / iterator.

Here, in this second part of the series, we will talk more about Ruby Data Structures, how it works & how we can iterate through it.

Array: Collection | List | Data Structure

Imagine you want to store the integer 1 in a variable. But maybe now you want to store 2. And 3, 4, 5 …

Do I have another way to store all integers that I want, but not in millions variables? If I asked this question, it probably has another way to store it.

Array is a collection that can be used to store a list of values (Like these integers that you want). So let’s use it!

It is really simple. We created an array and stored it on my_integer.

But maybe you are asking: “How can I get a value from this array?”

Great question. Arrays has a concept called index. The first element gets the index 0 (zero). The second gets 1, and so on. You got the idea!

For better understand, we can represent the array and each element with its index. Should I draw it?

Using the Ruby syntax, it’s also simple to understand:

Imagine that you don’t want to store integers. You just want to store strings. Like a list of your relatives names. Mine would be something like that:

Works the same way as integers. Nice! :)

We just learnt how Array indices works, but I need to show you about how we can add an element to the Array data structure (an item to a list).

The most common methods to add a new value to an Array is push and <<. Let see how it works:

Push is super simple! You just need to pass the element (eg. “The Effective Engineer”) as the push parameter.

<< method is also super simple! You also need to pass the element as a parameter. You may ask “But it doesn’t use the dot notation as other methods. How it could be a method as well?” Wow, nice question! :)

Writing this:

..is similar to writing this:

Ruby is so great, huh?! ❤

Well, enough of Arrays. Let’s talk about another data structure.

Hash: Key-Value Data Structure | Dictionary Collection

Now we know that Arrays are indexed with integer numbers. But what if we don’t want to use integer numbers as indices? Some data structure that we can use numeric, string, or other types of indices.

Let’s learn about the Hash data structure. Hash is a collection of key-value pairs. Something that looks like it:

The key is the index pointing to the value. How do we access the Hash value? Yes, you are right. Using the key! Let’s try it.

I created a Hash about me. My name, nickname & nationality, and those attributes are the Hash’s keys.

As we learnt how to access the Array using index, we also use indices (keys in Hash context) to access the value store in the Hash.

In the example I printed a phrase about me using all the values stored in the Hash. Pretty simple!

Hmm, another cool thing about Hashes is that we can use anything as the value. In the Hash I created, I want to add a key “age” and my real integer age in it.

A key (“age”) value (24) pair using string as the key and integer as the value.

As we did with Arrays, let’s learn how to add elements to a Hash. The key pointing to a value is a big part of what Hash is, and so it is when we are talking about adding elements to the it.

We just need to assign a value to a hash key. Nothing complicated here, right?

Iteration: Looping Through Data Structures

As we learnt in the Ruby Basics, the array iteration is very simple. We, Ruby developers, commonly use each iterator! Let’s do it:

So the each iterator works passing each array element as a parameter in the block. For each element, we (can do everything with it) print it.

For hash data structure, we can also use the each iterator but passing two parameters to the block: the key & the value.

This is an example on how to use. We did name the two parameters as key and value, but it is not necessary. We can name it with “anything”. Let’s see it:

We can see we used attribute as a parameter for the Hash key, and it works properly! Great!

That’s it!

We learnt a lot of things about Ruby Data Structures:

  • Array: Collection | List
  • Hash: Key-Value Collection
  • How we can iterate through this data structures

And that’s it guys! I want to update this article. The idea is to share a great content and the community helps improve it! ☺

I hope you guys can appreciate the content and learn more about how Ruby works. And for the third part of Beautiful Ruby series, we are gonna learn about Ruby Object Oriented Programming Part 1.

If you want a complete course, learn real-world coding skills and build projects, try One Month Ruby Bootcamp. See you there! ☺

Have fun, keep learning & always coding!

I hope you liked this content. Support my work on Ko-Fi

My Twitter & Github. ☺

--

--