Today, I had my first assessment and it went alright I did fumble a lot. I got back up, and I am going to try again with my newfound knowledge of class and instances. This eluded me during the assessment, but a class is the template or the “idea” that we used to create objects in the program that we are going to make. When developers work on a project they need a blueprint so they know the direction they want to proceed in. In Ruby it is the same, a class contains the instructions for creating those objects as well as having the ability to create said objects. When a person uses .new on a class we create a brand new object using our blueprint, but you may think how does that object work, well thats where the instance part comes in. An instance is the single occurence an object, when we as programmers use instantiate we breathe life into an object and that becomes the individual object produce from the class. In coding, we have class variables, methods, and scopes and we also have the instance version of these things.
Class Variable vs Instance Variable: First lets start with variables, a class variable, which uses @@, are variables that are shared between a given class and all of its subclasses whereas an instance variable, which uses @, is only accessible in any instance method in a particular class and only belongs to that class. This means that the value that is held by the instance varibale is specific to whatever instance of the class it belongs to. These instance variables can also be called class instance variables since they belong to that class. A class variable on the otherhand as I mentioned before is available among all the objects as long as its been initialized; another way of thinking about this is that the class variable is global within the context of a single class and the instance is local to an object. An instance variable is also responsible for holding information regarding the instance of a class and is accessible only to the instance while a class variable is accessible to the entire class it has a class scope and store information regarding the class as a whole.
Class Methods vs Instance Methods: A class method provides functionality to the class itself and operates on a class as a whole but has no access to a particular instance’s variable unless you make the instance a variable. Instance methods are shared by all instances of the class, and they live in the class object. As I mentioned before class methods are used for the functionality that is not specific to any instance of class. The instance method is different because you use when you are required to act of a specific instance of class
Class Scope vs Instance Scope: A scope defines where the variable in a program is accessible from, although ruby has five scopes: constant, local, global, class, and instance I will talking about class and instance. I mentioned in the variable section that when writing a class variable you use @@. This is the class scope which allows it to be shared among all instances of the class. If you change the value of the variable the new value will change all the other variables as well. An instance scope uses @ when writing instance variables. These are similar to class variable, but instead of being shared among the class its values are local to the specific instance of an object.
Whew, I think I talked about a lot, but let’s cover our final topic today: Attr_accessor with this example. In Pokemon, we have Kyogre who rules over the water and Groudon who rules over the land. Both of these pokemon have abilities to shape the respective regions as they please. They are both similar to attr_accessors since they have the ability to access to the water and land respectively and that is what attr_accessor is. It is a ruby module that has the ability to read and write another way you can look at it is attr_reader + attr_writer = attr_accessor. Kyorge and Groudon have the ability to read the water and land and then write and make edits to it. This is anotherway of using getters and setters that pertain to the same attribute.