Class Variables & Methods to the Madness

Tomazye’ anderson
4 min readMar 19, 2020

I’ve finally embarked on my software engineering journey and it’s been nothing short of madness! Thank God I have class variables and method’s to help me out! I also have awesome instructor’s at the Flatiron School which I now attend. It’s been a very interesting time so far, going through the process is much different than reading or hearing about it from other people. I had no prior knowledge of programming before starting this process but in a short amount of time I have learned A LOT !

We began by learning about object oriented programming in Ruby. This is basically programming with “objects” that hold data and code in the form of methods. Within object oriented programming or (OOP) we also have classes which are the blueprint for an object. It defines the data an object contains and the way it behaves.

We call methods defined within the class: “instance methods”. These classes can hold instance methods in which the objects will then respond to. Many different objects can be created from a single class. Within these instance methods we have a variable this is accessible which is called an instance variable. Normally you’d have a local variable but it’d bound to that method, in order to get around this we use instance variables.

Let’s say we wanted to store values related to a class rather than a particular instance!!

In this case we would use our wonderful class methods and class variables. The goal is to always try and make our code “DRY” (Don’t Repeat Yourself).

Class methods and class variables allow for added functionality. Let’s say you wanted to keep the count of something or return the allotted amount of an array of elements. You would have to figure out who’s responsibility it is for this behavior.

Class methods and class variables help with this and are cool because they allow you to call on that class itself and not the instances of that class. Class variables have class scope, which basically describes where in a program a variable is accessible. Ruby has four types of variable scope, global, local, instance and class. It is written with the double “@@” symbols and similar to an instance or a local variable, you can set it equal to any type of data.

We’ve made progress but we still need a way to access our class variable. This is where the class method comes into play. We describe our class methods with the “self” keyword. Using the self keyword here we are referring to the entire class itself. This defines our class scope rather than defining our instance scope.

Here if we call Album.count it will return us 0. That’s great but what if we want to increment this value ? We’d like for the count of albums to go up once a new album is created.

In order to accomplish this we’d use what’s called an #initialize method. We could then define our class variable within it and set it to increment. Mainly our initialize method allows us to set the initial values for an object.Whenever Ruby creates a new object, it looks for a method called #initialize and executes that method.

These are the basics and core things to consider when it comes to class methods and class variables along with how they’re implemented. Hopefully this provided some method to the madness in your coding journey.

sources : https://learn.co/lessons/ruby-class-variables-and-class-methods-readme

--

--