Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Classes in Ruby?

If you are learning programming, you might have come across the term "classes" in Ruby or any other object-oriented programming (OOP) language. In this blog, we will explore the concept of classes, their importance in OOP, and how to use them in Ruby. We will also provide code examples and helpful analogies to make it easier to understand.

What are classes?

In the world of programming, we often deal with various types of data, such as numbers, strings, and arrays. Each data type has its own set of properties and behavior. To make it easier to work with these different data types, we group them into categories called "classes."

A class can be thought of as a blueprint for creating objects. An object is an instance of a class, and it has its own set of properties (called "attributes") and behavior (called "methods"). In Ruby, almost everything is an object, including numbers, strings, arrays, and hashes.

Here's an analogy to help you understand the concept of classes and objects better: imagine you are designing a video game, and you need to create characters. You can create a class called "Character" that defines the common properties (like name, health, and level) and behavior (like attack, defend, and move) for all characters. Later, when you need to create actual characters for the game, you can create instances (objects) of the "Character" class, and each object will have its own set of attributes and methods.

How to create a class in Ruby

Creating a class in Ruby is simple. Just use the class keyword followed by the name of the class, and then end to close the class definition. The name of the class should be in CamelCase, which means the first letter of each word should be capitalized, and there should be no spaces between words.

Here's an example of how to create a class called Person:

class Person
end

This class doesn't do anything yet, but it serves as a blueprint for creating Person objects.

How to create an object in Ruby

Once you have a class, you can create objects (instances) of that class using the new method. The new method is a built-in Ruby method that creates a new object of a class.

Here's an example of how to create an object of the Person class:

person = Person.new

Now we have a person object, which is an instance of the Person class. However, this object doesn't have any attributes or methods yet. Let's add some!

Adding attributes to a class

Attributes are properties of an object. In our Person class, we might want to add attributes like name, age, and gender. To do this, we can use "instance variables" inside the class definition.

Instance variables are variables that belong to an object and are prefixed with an @ symbol. For example, @name is an instance variable that represents the name attribute of a Person object.

Here's an example of how to add attributes to the Person class:

class Person
  def name=(name)
    @name = name
  end

  def age=(age)
    @age = age
  end

  def gender=(gender)
    @gender = gender
  end
end

We added three "setter" methods to the class: name=, age=, and gender=. These methods are used to set the values of the instance variables.

Now we can create a Person object and set its attributes:

person = Person.new
person.name = "Alice"
person.age = 30
person.gender = "female"

Adding methods to a class

Methods are functions that define the behavior of an object. In our Person class, we might want to add methods like greet and celebrate_birthday.

Here's an example of how to add methods to the Person class:

class Person
  # ... (attribute setter methods)

  def greet
    puts "Hello, my name is #{@name}!"
  end

  def celebrate_birthday
    @age += 1
    puts "Happy birthday, #{@name}! You are now #{@age} years old."
  end
end

We added two methods to the class: greet and celebrate_birthday. The greet method prints a greeting message, and the celebrate_birthday method increases the person's age by 1 and prints a birthday message.

Now we can create a Person object, set its attributes, and call its methods:

person = Person.new
person.name = "Alice"
person.age = 30
person.gender = "female"

person.greet # Output: Hello, my name is Alice!
person.celebrate_birthday # Output: Happy birthday, Alice! You are now 31 years old.

Inheritance in Ruby

Inheritance is a powerful feature of object-oriented programming that allows you to create a new class based on an existing class. The new class is called a "subclass" or "derived class," and the existing class is called the "superclass" or "base class."

Inheritance is used to reuse code and model relationships between classes. A subclass inherits the attributes and methods of its superclass, and it can also have its own attributes and methods.

To create a subclass in Ruby, use the < symbol followed by the name of the superclass.

Here's an example of how to create a Student class that inherits from the Person class:

class Student < Person
  def school=(school)
    @school = school
  end

  def study
    puts "#{@name} is studying at #{@school}."
  end
end

We added a school attribute and a study method to the Student class. Since Student inherits from Person, it also has the name, age, and gender attributes, and the greet and celebrate_birthday methods.

Now we can create a Student object, set its attributes, and call its methods:

student = Student.new
student.name = "Bob"
student.age = 20
student.gender = "male"
student.school = "Ruby University"

student.greet # Output: Hello, my name is Bob!
student.study # Output: Bob is studying at Ruby University.

Conclusion

In this blog, we learned about classes in Ruby, how to create and use them, and how inheritance works. Classes are an essential concept in object-oriented programming, and understanding them will help you to write more efficient, organized, and reusable code. So, keep practicing and experimenting with classes, and soon you'll be able to create powerful and complex applications in Ruby.