I’m very exited that there’s only one week left until my mentor classes at airpair begin!
Since my learning will coincide with weekly projects, I wanted to explore to better understand Object Oriented Programming, or OOP. For this post, I think it best to omit discussing more advanced topics such as Object Oriented design patterns, and instead, to nail down the basics, one brick at a time. Lets get started!
A class in Ruby is just a way to organize and create objects with similar methods and attributes. The Ruby 2.0 documentation is a great place to begin to learn about classes, and is also the default place of reference before asking a friend or heading over to stackoverflow.
Here’s what the docs have to say about what happens when a new class is created:
Typically, you create a new class by using:class Name # some class describing the class behavior end
When a new class is created, an object of type Class is initialized and assigned to a global constant (
Namein this case).
You may or may not have heard it a million times, in either case, a class is similar to a factory. Its purpose being to build composites of what the factory was designed to assemble. This isn’t always the case with classes, but for the scope of this blog post, and the limits of current my understanding :), I’ll leave that alone.
So, we build our class or factory in order to have an ability to create an object out of the pieces that the factory helps put together. Lets see how that looks.
The example above reads similar to the documentation example preceding it. But instead of making a Name factory, we’re going to make an Automobile factory.
In Ruby, as in other languages, there is a way to create an object from a class, where we can pre-set, or better yet, initialize what comes default, or ‘stock’ with every object ever assembled in that factory. In languages such as PHP this construct to build default characteristics into your objects are called constructors. In Ruby however, there is the ‘initialize’ method.
Here’s a simple example demonstrating how to utilize the ‘initialize’ method:
The ‘@’ symbol listed above refers to a specific type of variable in Ruby. There are actually four types of variables in Ruby. The differences between variables refer to the variable’s scope, or where the variable is accessible to the program.
Because I have personally misinterpreted at least two of these variable scopes rather frequently, it’s certainly a good idea for me to briefly explain each variable scope, if for no other benefit than that of my own.
The four variable scopes are local, global, instance, and class.
A local variable is only accessible in the code construct in which it is declared. I’m a visual person, so here’s a quick example of what that could look like.
In the above example, ‘age’ is the local variable. From what I understand, this means that ‘age’ will not be accessible outside of ‘my_method’. Meaning you could not call it from any place outside of its current code construct.
Global variables are recognized by the preceding ‘$’ sign and are accessible throughout the entire program, no matter where they were declared, thus the term global. I have yet to read anywhere about global variables without this-or similar sounding- warning attached.
The usage of global variables are strongly discouraged.
From my understanding this happens, due to the fact that global variables can also be changed anywhere in your program. So for now, I don’t ever worry about using them at this stage of my learning.
A Ruby class variable has the two ‘@’ symbols preceding the variable name. And sort of like it sounds, a class variable in Ruby is one variable value accessible to all instances of the class, so every object instantiated from our Automobile class would have access to a class variable if we did something like so:
Here I defined a class variable that holds the value for a default fuel source for all automobile objects that will ever be created. I’m not certain about the design aspects of adding a class variable for such a thing in real life, but I it may suffice to illustrate a class variable in action.
And finally, we come to instance variables. Instance variables are similar to class variables except that its scope is localized to each particular object created by the class. This means that we could create any number of objects (lets say four for this example), and each object could hold a different, but more importantly, unique value for ‘@type’. The instance being tied to each object created.
Tomorrow, I will continue this train of thought about classes. How do we actually create these objects I talked so much about? What other features can be built into a class?