The concept of objects and classes
Objects have some amount of data inside themselves and govern the access and behaviors around that data. They are like actors, communicating with other objects by calling methods and exchanging data in a very defined interface. No object is allowed to interfere with the internal state of another object directly – methods define all interaction.
Classes are the blueprints that objects are created from. Every object is an instance of some class. The class defines the data layout, the available methods, the behaviors, and the internal implementation. The class of an object is often referred to as its type: every object has a type.
In Crystal, everything is an object – every value you interact with has a type (that is, it has a class) and has methods you can invoke. Numbers are objects, strings are objects – even nil
is an object of the Nil
class and has methods. You can query the class of an object by calling the .class
method...