Keeping it Classy in Ruby!
Classes in ruby or any Object Oriented Programming are an amazing simple idea that can create a whole library of objects. The idea of classes can be easily thought of like a car. In class car we have different attributes that all cars have in common, a steering wheel, tires, seats, engine, etc. We can change the value of those attributes but they all still have them. We also have methods within that class, as far as class car is concerned what are some functions or methods of a car? The car can "go", "stop", "turn", etc. The point of creating a class for "car" is so that we can create objects of the class car without redefining what a car is every time we do so. Since we know that "cars" share these common characteristics and functions we can make a blueprint of sort for cars and how they can behave. This gets rid of a lot of repetition in your code and can make it much more D.R.Y.. Only "cars" will be able to be a part of this class because it is set up specifically for them but other classes can be created for other objects. When we set up our car class we will create an initialize function that will create the instance variables necessary for our "car" objects. Below is an example:
class Car def initialize(input) @model_year = year # model year of car @gas_mileage = mileage # gas mileage of car @top_speed = speed # top speed of car end end
Methods to a higher class!
Now that we have created instance variables for our class that each new object of class Car will automatically be given we can add a few methods to the madness (pun intended). We now know that our car has a model_year, gas_mileage, and top_speed but how about if we want the car to "go", "stop" or "turn" like we talked about earlier. This is where our methods can come into play within our class. The beauty of class is that it can hold as many methods we would like for our cars to be able to access. These methods will only be accessible to objects within the car class and so they can be very specific and called on any objects of the car class that we create. Here is an example:
class Car def initialize(input) @model_year = year # model year of car @gas_mileage = mileage # gas mileage of car @top_speed = speed # top speed of car end def go(speed) # method code block goes here end end
Why class is so important!
Ruby has many classes built in that we have been using all along in this journey, whether we realized it or not. We have seen several different types of classes of objects already including: strings, arrays, floats, integers, hashes, times, etc.. Here is some examples:
x = String.new or x = "Cherry Berry" newArray = [] or new = Array.new newHash = {} or new = Hash.new y = 824 # integers cannot use Integer.new, you just have to write the integer
As we saw earlier in our class Car we set up variables in our initialize function preceeded with the @ symbol. What this is doing is specifying them as instance variables, this is literally making those variables an object of the class Car that can be used throughout the class and last as long as the object that we create does. Local variables within methods only last as long as the method lasts, instance variables however last as long as the actual object and stick with it. These variable's are saved and can be used throughout the program to call values. Pretty cool eh'! As you can hopefully see classes are a key idea in Object Oriented programming and help us keep our code D.R.Y. and open ended for the amount of objects that can be created. We use classes in our everyday lives all of the time and they are a great tool with programming that needs to be understood thoroughly in order to program to your full potential. Thank you for reading and I hope this has helped you gain a further understanding of the power and importance of 'Keepin it Classy'!