Struggling with Self??

Dolly Desir
3 min readJun 17, 2020

--

Definitely strugglin with self confidence, definitely living with self doubt through this process but I’m talking about another self….self instances and self class methods. The concept of Self for me was like Mayweather vs McGregor.

I did not understand self, I did not understand how ‘Self.whatever_method’ was different from just a stand alone ‘self’, for example Magazine.all << self. It wasn’t until I had a very informative zoom call with a classmate who really took the time to break it down to me. So hopefully this breakdown will help someone else. I plan to add to this later, showing how to test your methods.

Let’s get into it! I will use the example of a Magazine, Article and Author. A magazine can have many articles, articles can have many authors, but how do I ask Ruby for all the articles of a particular author??

First I will discuss self in this part of my code. I’ve created my Author class and initialized it with a name attribute. Within my initialize, on line 54, what is self doing? I’ve all called my Author class and called .all on that class and ‘<< self’ is putting all of my instances of an author into my Author class. What the hell do I mean you say? Well all of my authors are initialized with a name, so Jane is an instance of an author, Paul is instance of an author and every time a new instance of any author with any name gets created, I’m asking Ruby to put them in the Author class.

Next let’s move to line 56, ‘self.all’ is exactly the same as ‘Author.all’, I am calling on line 54 basically. Whenever you see self.whatever_method, this is a Class method. Then on line 57 I’m telling Ruby to put all my authors into this @@all variable that I defined on line 50. So when I need to look through my authors I’m going to call ‘self.all’ and Ruby will look through that array.

Finally this is where we ask Ruby to only return the article or articles that a particular author wrote. Remember our ‘Author.all’ that we shoveled all of our authors into, well there’s an ‘Article.all’ and it holds information about my authors as well. So on line 67, I am asking the Article class which was initialized with an author in a separate file, to look through all of its articles. Then on line 58, asking Ruby to go through all of its article instances, look through all the authors and give me all of Jane’s (self).

So inclusion, I eventually became Mayweather and ‘self’ became McGregor, and I’ve been whoopin self’s a…….sorry I got a little carried away. What I found helpful in understanding what code is doing, I read it out loud, explaining what each single word is doing. I am definitely not the best at this coding thing but I had to find ways to help me understand. There are certain patterns that are repeated in coding but memorization is not the way to learn this.

My self confidence fluctuates depending on what I’m doing as far as coding. Self doubt is something that I think I will probably live with forever but we will just go rounds for rounds like a boxing match, one round it might win but I plan on winning most rounds.

--

--

Dolly Desir
Dolly Desir

No responses yet