Day 10 at Makers Academy

More Test Driven Development

Today we were tasked with working on the Student Directory challenge, creating a basic app that runs in the terminal. Luckily for me I had already completed it on Day 8 as part of some extra work I had been doing. Because of this I took the chance to go off script and develop my app a bit further. The app is saved here. The guide ended after option 4, but advised us to number the Exit option as number 9, so that left 5 – 8 blank. I decided I would try to add a couple of options in myself. The first option I decided to add was the ability to remove names from the directory. I managed to do this fairly quickly I am happy to say. After that I looked back at my other options and thought that the ability to save was a bit bare. You could save a file, but there was no way of knowing if that file name existed already and if you were overwriting other information. I added checks in to make sure the file was not already created, and if it was, added the option to either overwrite it or add onto it. Google helped me with the latter, I discovered the File.open(filename, “a”) option as a way to add information onto the end of a file.

At this point I wanted to put some of yesterdays work into practice and run some tests. TDD is meant to work by writing the tests first, in a basic way, and building your code from there. Then you go back to your tests, rinse and repeat. As I had already built the app I was going to work a little different, I knew what my code did, so I had to write tests to make sure it was doing it correctly and not causing issues. To start I also had to define a class and move all my methods inside of it, as we had yesterday in the TDD workshop. I also knew that attr_reader was very useful when working within classes and between methods, but I didn’t fully understand what it actually was or did.
This helpful video was great at explaining it with simple examples. Using this knowledge I added my attr_reader variable in and adjusted my methods to account for this. I had written 6 tests for the code before hitting a block:

  • Gives menu options – does it give you menu options when you run it? Tick
  • Lets you add students – are you able to add students and does it store them? Tick
  • Lets you count students – keeps count of the amount of students in the list? – Tick
  • Lets you remove students – can you remove a student from the list? Tick
  • Load students from a csv file – can you load students from existing csv file? Tick
  • Save students in a csv file – can you save students into a csv file? Kind of tick…

The last one was a bit of a problem, it worked fine if a new csv is created, however if for example it runs the test for a file called “test.csv”, and that file already exists, the test breaks, as it asks you whether or not you want to overwrite it. I fixed this by separating my 3 save options into 3 separate methods, and only running the test on the method which saves a new file. I also added a line of code at the end of the test to delete the test.csv file. As I figured this out I wrote two more tests:

  • Lets you overwrite existing csv files – can you save over files with the same name – Tick
  • Lets you add to existing csv files – can you add information onto the bottom of an existing csv file – Tick

After this I decided to tackle option 6, which I decided should be the ability to delete a csv file completely. This was fairly easy as I had already been deleting csv files in my tests. I added in a double check so that you have to confirm you want to delete the file just to be safe, and also a check that returns a message if the file doesn’t exist. I added a test in for this too:

  • Lets you delete a csv file – can you delete a csv file using filename? – Tick

Later in the day one of the Makers coaches gave me a tip. Instead of having to use a variable to initialize a new class in every test, you can use ‘subject’.
For example, instead of using:
variable = Class.new
variable.method(parameter)
You can just do:
subject.method(parameter)

I added one more option after this, number 7, clear the list. Very simply, clears anything from the array and gives you a blank list. Very easy to write, very easy to test, and very useful for my other tests!


Last night I read an interesting blog by Hongli Lai, and even though many of the concepts are far beyond my current knowledge, it was really cool to find out more about how memory works in the computer, and how it is shared around. My favourite part was this:

“If only there is a way visualize the OS heaps so that I can see what’s going. Unfortunately there are no tools that allow me to do that.
So I wrote an OS heap visualizer myself.”

Hongli Lai

That seems so complex to me, yet he’s essentially just done it for a laugh. I can’t wait until I get to that level of knowledge!


Another short blog I read last night was by Victor Zhou, and was about why he became a programmer, which I enjoyed, as it shows there’s so many reasons people come into this field of work.


I also did another challenge on Codewars, mainly because the next one it recommended me was essentially the reverse of one I had done previously, so I knew I could smash it out in about a minute! Still at 7kyu, but now have 50 Honor.

I’m coming for you Nikesh!

Leave a comment