I would like to preface this blog by stating I AM NEW TO CODING, oh and I AM NEW TO BLOGGING.
Coding is incredibly DRY(pun intended.) When I say that I mean, it’s just numbers and letters on a screen. There isn’t anything stimulating on the screen and for people like me that are visual learners, understanding material is incredibly difficult. I can’t learn what I can’t picture, therefore in my mind I have to ‘visualize’ what the code I’m writing does. For example, let’s take a Class (or a thing or an object) like Horses!
In code world, this is what it would look like:
But what I essentially did was make three horses that are incredibly silly :
Now as you look at the code, you can see that each horse has unique qualities, but they are ultimately a horse, just a different kind (of instance.) Now what we can do is play with our horses and make them do anything we want really. For example, let’s say Gortat wanted his friends to have the same color hair as him, so he feels less left out (and because he’s selfish and doesn’t want to change his own hair color.) We would write this out in code like so:
def make_everyones_hair_look_like_mine Horse.all.each do |horse_instance| horse_instance.hair = self.hair endendgortat.make_everyones_hair_look_like_mine
Essentially, when looking at the last line Gortat calls to the Horse class and calls the method “make_everyones_hair_look_like_mine”
In Fun world what is happening is this:
Gortat calls to the horse gods, and tells them to change all of the horses hair color to his. Now this will only work, because we clearly have defined this as a thing that the horse gods can do. If there was no method, then we would get an error. In other words, as coders we are essentially playing God with our data. And if we don’t have the power to do their request, then we just don’t.
So when Gortat makes this statement all of sudden POOF!
To break this down a little bit more. Here’s the code again:
def make_everyones_hair_look_like_mine Horse.all.each do |horse_instance| horse_instance.hair = self.hair endend
So what we are essentially doing is going through all of the horses we have created with our Horse.all method we created earlier. We want to go through each of them and re-assign their hair color. When running enumerable’s and iterating through them. (In other words, when we do ‘each’ we are going through each horse one by one.) In this case “horse_instance” is each horse and all of their information. In code world that looks like this:
#<Horse:0x00007f8140986070@color="white",@hair="blonde-ish",@name="gortat",@sillyness="maybe a bit to much">
After checking Gortat, ‘.each’ will then move on to Chauncey, and then Mr.Censored.
So now that we have all of his information, we can write “horse_instance.hair” This will only return the information on the particular horses hair color. In this case our iteration starts off with Gortat.
Nice, Now all we have to do is assign that hair color to everyone else's. A fun thing about the “=” sign in code world, it is the assigner. It assigns properties, as a beginner a little trick to remember its duty is just using the words “is now” every time you assign something. For example,
horse_instance.hair = self.hair“Chauncey’s hair is now my hair”
"Censored's hair is now my hair"
"Gortat's hair is now my hair"
What this line of code is doing is, taking whoever this horse_instance is. So it’s looking at either Gortat, Chauncey, or Censored. It takes their hair color by calling ‘.hair’ and assigning it to self.hair.
In Fun world what this code is doing is, taking Chauncey.hair (which is his hair color) and saying “Chauncey’s hair is now my hair.” The horse gods have the powers to do so (because we wrote the method to do so.)
Now, you might be going “Hey Ignas what is self?” well self is me(or in this case Gortat) Remember earlier when Gortat said “Make everyones hair look like mine.” Well he’s the one calling to the horse gods, therefore he’s himself! If Chauncey wanted everyones hair color to look like his, all he would have to do is call to the horse gods and state “make everyones hair look like mine.”
Yes I understand that this blog skipped over a lot of information in defining the Horse class and how it’s built including other bits as well. But, It’s here to help my fellow visualizers, to actually get an idea on what their code is actually doing.
P.S Thank you Niki Thomaston for the Illustrations!
P.S.S Yes thats me and my friends drawn out as horses.