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:
class User {
constructor(name) {
this.name = name;
}
}
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.
user = User.new("Bilal")
user.greet # "Hello Bilal"
Class Methods
These are called on the Class itself. In Ruby, we define them using self.
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.
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:
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:
Has an
initializemethod that takesnameandage.Has an
introducemethod that returns:"My name is NAME and I am AGE years old".Uses
attr_readerso we can see the name and age outside the class.Create one user instance and call the
introducemethod.
Solution:
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
Post a Comment