The student directory script allows you to manage the list of students enrolled at Villains Academy.
Student Directory
Providing a .csv file is optional
ruby directory.rb file.csvPrint File
(filename is optional)
ruby print_file.rb filename-
We're using the each() method to iterate over an array of students. How can you modify the program to print a number before the name of each student, e.g. "1. Dr. Hannibal Lecter"?
-
Modify your program to only print the students whose name begins with a specific letter.
-
Modify your program to only print the students whose name is shorter than 12 characters.
-
Rewrite the each() method that prints all students using while or until control flow methods (Loops).
-
Our code only works with the student name and cohort. Add more information: hobbies, country of birth, height, etc.
-
Research how the method center() of the String class works. Use it in your code to make the output beautifully aligned.
-
In the input_students method the cohort value is hard-coded. How can you ask for both the name and the cohort? What if one of the values is empty? Can you supply a default value? The input will be given to you as a string? How will you convert it to a symbol? What if the user makes a typo?
-
Once you complete the previous exercise, change the way the users are displayed: print them grouped by cohorts. To do this, you'll need to get a list of all existing cohorts (the map() method may be useful but it's not the only option), iterate over it and only print the students from that cohort.
-
Right now if we have only one student, the user will see a message "Now we have 1 students", whereas it should be "Now we have 1 student". How can you fix it so that it used singular form when appropriate and plural form otherwise?
-
We've been using the chomp() method to get rid of the last return character. Find another method among those provided by the String class that could be used for the same purpose (although it will require passing some arguments).
-
Once you have completed the "Asking for user input" section, open this file. It's Ruby code but it has some typos. Copy it to a local file and open it in Atom without syntax highlighting. To do this, select "Plain Text" in the lower right corner of the window.
-
What happens if the user doesn't enter any students? It will try to print an empty list. How can you use an if statement (Control Flow) to only print the list if there is at least one student in there?
- 8.1 Commit b91fc10
- 8.2 Commit b91fc10
- 8.3 Commit 8962484
- 8.4 Commit 084c573
- 8.5 Commit 7fe7779
- 8.6 Commit 0864582
- 8.7 Commit 1e860a3
- 8.8 Commit 4d9c574
- 8.9 Commit de22dbc
- 8.10 Commit cf2b93f
- 8.11 Commit 775261a
- 8.12 Commit 18e2fd1
-
After we added the code to load the students from file, we ended up with adding the students to @students in two places. The lines in load_students() and input_students() are almost the same. This violates the DRY (Don't Repeat Yourself) principle. How can you extract them into a method to fix this problem?
-
How could you make the program load students.csv by default if no file is given on startup? Which methods would you need to change?
-
Continue refactoring the code. Which method is a bit too long? What method names are not clear enough? Anything else you'd change to make your code look more elegant? Why?
-
Right now, when the user choses an option from our menu, there's no way of them knowing if the action was successful. Can you fix this and implement feedback messages for the user?
-
The filename we use to save and load data (menu items 3 and 4) is hardcoded. Make the script more flexible by asking for the filename if the user chooses these menu items.
-
We are opening and closing the files manually. Read the documentation of the File class to find out how to use a code block (do...end) to access a file, so that we didn't have to close it explicitly (it will be closed automatically when the block finishes). Refactor the code to use a code block.
-
We are de-facto using CSV format to store data. However, Ruby includes a library to work with the CSV files that we could use instead of working directly with the files. Refactor the code to use this library.
-
Write a short program that reads its own source code (search StackOverflow to find out how to get the name of the currently executed file) and prints it on the screen.
- 14.1 Commit 386283a
- 14.2 Commit 386283a
- 14.3 Commit 0bf91ed
- 14.4 Commit 8cd4cb7
- 14.5 Commit 1226df7
- 14.6 Commit 6ee83ee
- 14.7 Commit 2c5ef07
- 14.8 Commit 9eeb8c6
