Demystifying Data Structures in Ruby
The human being has been inspired by nature to develop ideas for a long time. Data structures are no different. It is a way of organizing and storing observations that have been abstracted from their real world environment. For instance, a classroom of students could be represented as an array of names. A student could also be represented as an object with endowed attributes (what they look like) and behavior (what they do).
Ruby gives us several collection types for us to use. Along with arrays and objects, there are also: hashes, sets, tuples, and structs.
Arrays
Arrays can hold anything that inherits from the Object class. An array is a collection separated by commas to maintain order.
Code 1. Working With Arrays.
Arrays make it easier to implement other data structures like: stacks and queues. The stack organizes data like a kitchen dishwasher. First, plates become our data. We organize the plates by taking one and putting it down on the table, one on top of the other. When we are ready to serve people, we grab the top plate first. The Queue organizes data like a grocery store line. Someone enters the store, gets their groceries, and then they go to the cashier, wait their turn in the line, pay and leave. We can model these scenarios with abstract methods such as: pop() and push, and shift() and unshift().
(LIFO - Last In First Out) (FIFO - First In First Out)
Figure 1. How a Stack Data Structure Works.
Code 2. Stack Data Structure Implementation in Ruby.
Figure 2. How a Queue Data Structure Works.
Code 3. Queue Data Structure Implementation in Ruby.
Objects (An instance of a Class)
Object-Oriented Programming gives us the possibility to create customized data structures. In the example below, we can see a blueprint of a person. From that definition it is possible to create instances of it.
By using classes we have a whole world of possibilities when it comes to develop data structures; we can build pretty much any type of struct and even create hybrid ones.
Hashes
Hashes are a data structure similar to arrays. Instead of relying on the item’s place in line, which we also call the index, we can use a key/value structure to store and retrieve data. The power is in the ability to find an item directly via its key. The ability to retrieve data by key allows for higher performance in working memory. (see code 4).
Code 4. Working With Hashes.
It is less efficient to use strings than symbols as keys. A simple test was performed to observe retrieval time (see code 5) for hashes using string and symbols as keys. It took ~ 1.17 seconds to retrieve 10,000,000 times the value using a string as key; and it took ~ 0.68 seconds using a symbol as key. The difference is ~ 41.02 % less time when symbols are used.
Code 5. Strings or Symbols as keys? You tell me
Sets
Sets are a special type of array. The major difference is that they enforce uniqueness. It also includes several methods related to the theory of sets (i.e.: union, merge, superset, subset, among others).
Code 6. Working With Sets.
Tuples (Rinda gem)
Tuples are available in Ruby through the Rinda gem.
It can be installed using the following command gem install rubysl-rinda
. Tuples, arrays and hashes are alike; they only differ in the fact that tuples are immutable. It will raise and error if we try to reassign an element.
Structs
Structs are a data structure that helps to bundle a number of attributes together. It is extremely useful when we need to group accessible attributes but we don’t need to have an explicit class.
Data structures are very important to manipulate and organize data. It makes it easier to abstract concepts from the real world.
Acknowledgments
Thank you Steven for helping me with the text proof-reading.
Thank you Michael for helping me to come up with a best post title.