Apprenticeship -- Day 6

December 3rd, 2018

Initialization Method

Last week, I had some questions about Ruby classes. I was confused by the initialization method. When would I need to use this method? How would I be able to test this? These were a couple of the questions that I had. I was having trouble with a TDD exercise. I needed to create a Person class and methods that would display a full name, full name with middle initial (if a middle name was provided), and initials. Since I knew that I would be expecting different parts of a name (first, middle, last), I created an initialization method. I've been using this method anyway since it seemed like the correct thing to do, or I'd seen it used in plenty of examples already so it seemed standard.

Here was my first attempt:

Screen Shot 2018-12-16 at 2.19.29 PM

And the test: Screen Shot 2018-12-16 at 2.20.06 PM

My tests were failing due to argument expectations, but if I deleted arguments for either method, my tests would still fail. My tests also failed if I didn't specify that my middle name argument would default to nil. So I asked my mentor about it. He said that there was some duplication for sure and that I would probably understand where after watching people complete katas. So I watched a few videos on YouTube, which led me to gain a better understanding of initialization.

Here was my passing attempt:

Screen Shot 2018-12-16 at 2.37.40 PM

I learned that classes could be created to have some initial data by using the initialize method. It appears that a class can provide a blueprint for constructing objects, and they can also be defined to house initial data or expectations with each new instance. In my example, I was able to set argument expectations, assign and save those those values as variables, and make those variables available to all the methods in the class.