Search⌘ K

Objects and State

Explore how objects and their states work in Ruby's object-oriented programming. Understand the role of classes as blueprints, the meaning of object instances, and how state differentiates each object. Learn about the initialize method and how it sets the initial state of objects to create functional Ruby programs.

We'll cover the following...

People think that Object-oriented programming (OOP) is something very complicated, magical, and difficult to understand. But in reality, it’s mostly straightforward. The right way of doing OOP could simplify the daily life of a programmer. However, it requires more brainpower than the common way of doing OOP.

This course covers the essentials, including the common way of doing OOP, which is a must for every beginner. If you’re looking for an elegant way of writing your object-oriented code, start reading Elegant Objects by Egor Bugayenko.

Class

Object-oriented programming assumes there should be an object somewhere. But what are objects? From our everyday lives, we know that everything is an object, like a book on the table, for example, or a man walking down the street, or BMW model E34 crossing the road. But if we look closer, BMW E34 is a certain class of objects. This particular BMW is the same as another of the same model. But at the same time, they’re different instances.

The most obvious example of a class is a technical drawing of a jewel:

We can see technical details of this jewel product like width, height, radius, and so on. We can see these details before the final product is manufactured. Class in a programming language is something similar to this drawing. It’s a pattern, ...