Overview

Welcome! Today, we’re diving into Ruby classes, the foundation of Object-Oriented Programming (OOP) in Ruby. This lesson introduces the concept of classes as blueprints for objects. We’ll explore how to create classes and objects, understand the difference between them, and see how attributes and methods store and manipulate data within each object.

Ruby Classes Refresher

In Ruby, a class serves as a blueprint for creating objects. Each object, or instance of a class, has its own data (attributes) and can perform actions (methods). Imagine a video game character: each character has attributes like health or strength, and methods like attack or defend.

Here’s a simple GameCharacter class to illustrate these ideas:

Here, GameCharacter defines a blueprint for characters. The attributes @name, @health, and @strength store data specific to each character, while the attack method defines behavior each character can perform.

Creating Classes and Objects

In Ruby, creating an object is as simple as defining a class and calling new on that class. Each object created this way is an instance of the class and has its own copy of the class’s attributes.

Each instance, character_1 and character_2, has its own unique data, demonstrating how a class can produce multiple distinct objects.

Understanding Attributes in Ruby Classes

Attributes, represented by instance variables prefixed with @, hold data specific to each instance. In the GameCharacter class, @name, @health, and @strength are instance variables storing each character's state. In Ruby, instance variables are private by default, meaning they cannot be accessed directly from outside the class. Instead, we need getter and setter methods to retrieve and update these values.

Using attr_reader and attr_writer

Ruby provides a convenient way to create getter and setter methods through attr_reader and attr_writer, allowing controlled access to instance variables.

attr_reader creates a getter method only, allowing an instance variable to be read but not modified from outside the class.

In this example, attr_reader provides read-only access to the @name variable, protecting it from unintended modifications.

Similarly, attr_writer creates a setter method only, allowing the instance variable to be modified but not read directly. This is useful for write-only data.

Here, attr_writer restricts access to writing only, making it ideal for sensitive information like a password or secret code.

Using attr_accessor

Ruby’s attr_accessor combines both getter and setter methods, allowing an instance variable to be both read and modified from outside the class.

In this example, attr_accessor simplifies access by enabling both read and write functionality, making it a versatile choice for variables that need full access.

Defining Methods to Add Behavior

Methods define actions or behaviors for each object. The attack method in GameCharacter defines a simple interaction between characters. By calling this method, one character can reduce another character's health.

In this example, attack modifies the health attribute of the target character, illustrating how methods operate on an object's state.

Example: A Simple BankAccount Class

To deepen our understanding, let’s look at another example, BankAccount, which models a real-world entity with attributes and methods. This class includes attributes like the account holder's name and balance, and methods for depositing and withdrawing money.

This BankAccount class shows how attributes and methods can encapsulate both data and functionality, making it easy to create multiple independent bank accounts with their own balances and behaviors.

Lesson Summary and Practice

In this lesson, we explored the fundamentals of classes and objects in Ruby. We learned:

  • How to define a class with attributes and methods.
  • How to create instances (objects) from a class.
  • How attributes store each object's unique data, while methods define its behaviors.

Now, it's time to move to the practice section to reinforce these concepts. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal