1. The Responsibilities of Class and Instance Variables — 2. Understanding Conditionals

Kendan Todd
3 min readDec 18, 2020
Source: https://www.thevoicecollege.academy/

Since starting at Flatiron Schools, I can say without a doubt that it has been the greatest challenge of my life. Every week brings new obstacles to overcome. Sometimes it is best to reflect on what it is I have learned to see how much I truly understand the material. Class variables, instance variables, and conditionals are three subjects I have decided to go back over as I realized I may not have understood them as much as I once thought.

Class Variables v. Instance Variables

As a class variable, these particular types of variables are accessible at any point in the scope of the class. This means a class variable can be accessed from any instance instantiated from that class.

As opposed to the more widely available class variable, instance variables are slightly more limited. Defined within the class, once an instance is instantiated with an instance variable, it is only accessible to that instance alone. For example:

In this scenario, @@all is a class variable and @name is an instance variable. @name will be responsible for containing the name of a new instance of the Person class. This means that @name is only responsible for that individual instance only. @@all is going to be responsible for keeping track of information of the class as a whole, including the instances of the class. Currently, since there are no instances, @@all is empty. But in the future, when instances are instantiated, we will be able to call upon @@all and access the names that it holds.

Conditionals

Conditionals can be described as the thing that controls the flow of the code. Conditionals can be formed within if-else statements and use the operators(<, >, <=, >=, ==, !=, &&, ||). Below is an example:

As you can see at the top, the variable “coffee” has an assigned value of “medium”. When coffee is plugged into the conditional, its value is evaluated. Depending on which conditional it matches will determine which message is displayed. As coffee equals “medium”, the message “Your coffee is a medium” will be displayed. Regardless of the input, there is a message to accommodate. As long as the input can match one of the sizes, Ruby will recognize the value as “truthy”, or it evaluates it as true. If the input does not match any of the options, the program will revert to displaying the message “You do not have a coffee” as no size was chosen from the user. This means that Ruby recognizes the value as “falsey”, or evaluates it as false.

--

--

Kendan Todd
0 Followers

Full-Time Software Engineering Student