Choose the Random Word
Explore how to read and filter dictionary files in Ruby by loading words, converting accents, and selecting random words. This lesson teaches you to manage word collections effectively, preparing you to build key game features.
Create a dictionary
If we want to offer different words, we need a list of words from which we can draw a word.
A simple way is to use a file containing one word per dictionary. Here is an example with several words:
#dictionary_fr.txt
a
Bonjour
en
été
In our example, we used French words and added a suffix (_fr) to the file name to show it contains French words. We must start by reading (loading) all words and filling a collection that we can then use to draw words. But we have some constraints:
- All the words must be in lowercase.
- We do not want to keep very short words.
- We do not want words whose letters contain accents. We must remove the accents because our game can’t tell the difference between a letter with an accent and the same letter without an accent.
Read the dictionary
We are going to isolate this task in a file in which we’ll add a routine to load the ...