Hashes Exercise

In case you get stuck anywhere, don't be afraid to ask the coaches! They are here to help and will gladly explain everything to you!

Take notes during the exercises. Even if you never look at them again, they will help you memorise things!

For these exercises, you will use your editor (Visual Studio Code or similar).

First, create an empty file with a meaningful name for this exercise, for example hashes.rb. Note the file extension .rb! Also remember what folder you saved your file in. It makes sense to create a folder for each lesson of the course.

Then, use cd in the command line to change to that folder. When you run ls you should see your file there.

Now you can run your Ruby program with ruby hashes.rb in the command line.

Repeat the above steps and remember to save your file in the editor before running it!

In this exercise, we’re going to write a primitive little translation program using a hash to store translations.

Choose any 2 languages you know. The example here will be with english and german.

  1. Create a hash variable in your code to store translations in. For example one key-value pair for a translation could be apple: "Apfel". Store a couple of translations in your hash.

  2. Now we want to access these translations. Write some code that lets the user enter a word in english and returns the german translation, like this:

Example interaction (user entered text is bold):

What should I translate for you? 
apple
The translation is: Apfel

Note: Because the keys in our hash are symbols, we need to convert the user’s input from a string to a symbol. Check the Ruby documentation for String for a suitable method.