Ruby is an Object orientated language, what that means is that pretty much (with some exceptions) everything is an object! Ruby has a feature that allows classification of all these objects for ease of use. Classes are Ruby's solution. What are classes? A class is just a way of organizing and producing objects with similar attributes and methods.
So now we know what a class is and that Ruby is full of objects! Lets dig a little bit more and demonstrate a real world metaphor for the elements in a class.
Do you remember your first car? I sure do so that's why we are going to build a class for car! Okay lets get started!!
Above is how you tell Ruby, "hey Ruby! I want to start a class and name it 'Car' ". (Keep in mind, that you have to capitalize the first letter of the class name). Not too bad right? Its nice and easy to make a class for car. Now lets fill that class up with some fun code!!
Above is "attr_accessor", that is Ruby's built in syntax. I know what you’re thinking what does that do? Well ... since you're so eager to learn I will tell you!! The "attr_accessor" tells Ruby that the following instance attributes are class properties, that can be called outside the scope of the class they part of.
Next up lets talk about some "instance variables". What is an "instance variable"? An instance variable is a variable that is attached to the instance of the class.
Notice anything familiar? Well if you see the names of the instance attributes they are the same for this initialize method! They may look the same but they do act different. The instance variable that you see with the "@" in front of it, is a instance of the class that can only be used within this class (Car). Where we have the instance attributes are used to access the properties of the class outside the scope of the class.
Next up on let's talk about "instance methods". An "instance method" is a method that lives within the class. Pretty much think of it like this, an instance method is just a function that can be called later with its method to tell the class what to do.
Above we made defined some instance methods for the class "Car". You can see that they act just like regular methods, where they can take in arguments!
Congratulations!!! You now have all the elements for your class method! So...what do I do now? Well now you can create a new instance of the class "car". Below we created a new instance of the class "car" with FordF150. We added the new car followed by "Car.new" with all the instance attributes from before!