Technical Blog: Ruby Classes
September 3rd, 2014
What are classes?
Since Ruby is an object-oriented language, its most prominent feature is the ability to create classes and objects. A class is simply a blueprint from which individual classes are created. The existence of classes gives us the ability to create, manipulate, compare and examine any number of objects at the same time, without having to write separate methods for each object by grouping together attributes and methods.
Class Example
Let’s look at an example of a "SportsCar" class:
The .new created the new instance of the class Sportscar while the initialize method gives the instance all of it's unique characteristics by assigning instance variables to each of the arguments.
What are instance variables?
Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name.
Now let's make this car move! Let’s start by making an instance method called "accelerate"! An instance method is a method that can only be called on an object from the class in which it is defined.