English-Spanish dictionary

Let's build a simple application in Ruby to practice what we have covered so far.

We'll cover the following

Let’s sum up everything we learned about hashes, arrays, and their combinations. We will build a minimalistic English to Spanish dictionary application. You can guess from the title which data structure we’ll be using: hash.

The most important thing here is a database. We will not use sophisticated database management systems like Postgres or MySQL. Instead, we’ll keep data organized in the data structure in the computer’s memory. It could look like this:

dict = {
  'cat' => 'gato',
  'dog' => 'perro',
  'girl' => 'chica'
}

We can also use any other data structure. In our case, it will be a set of key-value pairs, where the key is an English word in string_ type, and the value is the translation, a Spanish word string type.

Hash data structure allows us to perform fast lookups in our database, which means lookups in constant time or O(1). No matter how many words we have in the hash, lookup time will be about the same.

When we compare this approach to arrays, we can still solve this problem using arrays. However there are a few caveats. Here is what the code would look like:

arr = [
  { word: 'cat', translation: 'gato' },
  { word: 'dog', translation: 'perro' },
  { word: 'girl', translation: 'chica' }  
]

But to find elements in the array, we’ll need to iterate over each element and perform a comparison. The more elements we have, the more time we need for lookups. In other words, the number of elements we need to check grows along with the size of the array. The search complexity takes linear time, also represented by O(N), where N is the number of elements.

In real life, we probably won’t notice this difference for a small number of elements. Moreover, newer versions of Ruby use arrays instead of hashes for seven elements or fewer. We won’t see it, though, because usually programmers don’t need to poke around with language internals. However, anyone can open up Ruby language implementation, written with C, and confirm that.

Anyway, hash serves better for our purposes. When we use this or another data structure, we demonstrate our intention to other programmers, saying, “We’re using this specific data structure, and we assume that we’re going to use this data structure in a specific way.” How we implement our programs will “dance” around decisions we made initially. An important step is to pick the right data structure at the beginning.

One can look up an element in an array by providing an index, which is a number ranging from zero to the size of an array. In the case of a dictionary application, we don’t know these numbers. We know words. We have a key word, and we need to find out the value word. Here is how we perform lookups in the hash:

dict[input]

The minimalist application would look like:

Note: You can enter cat, dog, or girl in the input box below.

Get hands-on with 1200+ tech skills courses.