Help about classes and stuff


#1

I've finished the course and I know the syntax, its just that I don't understand what classes are and the concept of it. What is the difference between classes and methods. I also don't understand class, instance and global variables. I feel pretty confused about the whole course and I don't want to start the next course if I don't understand this one.
If anyone is willing to help me and explain this to me I would be very grateful.

Thank you very much!


#2

Ruby is a language entirely based on the concept of objects: methods are objects, hashes are objects, integers, strings, etc... Everything you've used up until now are objects (well almost everything).

These objects are not coming out of nowhere, they are defined somewhere in Ruby. Each object is defined by its class. For example, strings are defined by the class String. A class is sort of the blueprint of an object if you want a physical world analogy. It describes how the object is going to be used, which methods will it responds to, etc...

When you're asked to create a class, you're asked to describe an object and how it will be used, but that's it. If you want to use an object like the one you described, you need to create it by assigning it to a variable, for example:

my_variable = My_class.new

The new method will read the class and create an object from it, it's called the instance of an object.

When you have your instance, you can then manipulate your object as you want, just as you did before with arrays and other stuff.