Lesson 3: Ruby OOP (The DNA of ActiveRecord)

If you’ve spent your time in React writing functional components and hooks, the word "Class" might feel like a blast from the past. However, in Ruby on Rails, Classes are everything.

Every database table you create will be managed by a Ruby Class. Understanding how these objects work is the key to mastering ActiveRecord (the Rails database layer).


1. Class vs. Object (The React Analogy)

Think of a Class as your component definition—it’s the blueprint.

An Object is like a specific "rendered" instance of that component with its own unique props.

  • Class: User (The blueprint for all users)

  • Object: user_1 (A specific person named "Ali", age 30)


2. The initialize Method (The Constructor)

When you create a new instance of a class in Ruby, it calls a special method named initialize. This is exactly like the constructor in a JavaScript class.

JavaScript:

JavaScript
class User {
  constructor(name) {
    this.name = name;
  }
}

Ruby:

Ruby
class User
  def initialize(name)
    @name = name
  end
end

πŸ’‘ The Magic of @ (Instance Variables)

In Ruby, we use @name instead of this.name. These are called Instance Variables. They live as long as the object lives and are accessible inside any method within that class.


3. Instance Methods vs. Class Methods

This is a distinction that trips up many frontend developers.

Instance Methods

These are called on a specific instance.

Ruby
user = User.new("Bilal")
user.greet # "Hello Bilal"

Class Methods

These are called on the Class itself. In Ruby, we define them using self.

Ruby
class User
  def self.all_species
    "Human"
  end
end

User.all_species # "Human"

Why this matters for Rails: In a few lessons, you’ll see User.all or User.find(1). Those are Class Methods used to query the database!


4. Getters and Setters (attr_accessor)

In JavaScript, you can just do user.name. In Ruby, variables are private by default. You cannot see them from outside the class unless you explicitly "expose" them.

Instead of writing manual getter/setter functions, Ruby gives us shortcuts:

  • attr_reader: Allows you to read the variable.

  • attr_writer: Allows you to change the variable.

  • attr_accessor: Allows both.

Ruby
class User
  attr_accessor :name
  
  def initialize(name)
    @name = name
  end
end

user = User.new("Bilal")
user.name = "Ali" # Works because of attr_accessor!

5. Why This Matters in Rails

When we get to Rails, your models will look like this:

Ruby
class User < ApplicationRecord
end

By inheriting from ApplicationRecord, Rails automatically gives your class all the attr_accessors for your database columns, plus a bunch of powerful Class Methods to search and save data.


🧠 Mini Exercise

Let's build a basic "Model" logic. Create a class User that does the following:

  1. Has an initialize method that takes name and age.

  2. Has an introduce method that returns: "My name is NAME and I am AGE years old".

  3. Uses attr_reader so we can see the name and age outside the class.

  4. Create one user instance and call the introduce method.

Solution:

Ruby
class User
  attr_reader :name, :age

  def initialize(name, age)
    @name = name
    @age = age
  end

  def introduce
    "My name is #{@name} and I am #{@age} years old"
  end
end

me = User.new("Bilal", 25)
puts me.introduce

What’s Next?

In Lesson 4, we wrap up our Ruby foundation by looking at Blocks and Yield. This is the syntax that makes Rails feel "magical"—once you understand it, the magic disappears and you'll see exactly how the framework handles logic.


Ready for the final Ruby foundation lesson? Reply with "START LESSON 4"!

 

Comments