Exercise on reading a file in loop
You need to create a new folder inside your exercise repository and name it files
, then with Ruby you have to:
- Create a file inside that folder and name it
hello.txt
- Print
Hello
15 times in 15 different lines - Close and save the file
The file should look like:
Hello
Hello
...15 times
Now double check the content of that file, if it has the right content, then here you go.
You need to read the file you created in the previous exercise (hello.txt
), then the file should look like:
Hello World!
Hello World!
...15 times
Now what about generalizing the code so we can add whatever words to the end of each line we have in that file? So for example say you want to print your name at each of the lines, the output will be:
Hello World! Islam!
Hello World! Islam!
...15 times
Now what if we need to dynamically change the number of lines, so say you want to print the lines only 4 times, then you want to re-run your code so it prints it 9 times. We need to generalize our code even more so we give how many lines we would print.
Example:
You want to print your name (Ahmed
) 5 times, the output will be:
Hello World! Ahmed
Hello World! Ahmed
...5 times