Classes and Objects
Fast recall
Quick revision
Core answer
Book.new(...) creates an object and normally runs its initialize method. Instance variables such as @title belong to that object, while instance methods operate with that object as self.Whether you know a class is a blueprint, an object is an instance, new calls initialize, and @vars hold state.
Forgetting to set @vars in initialize (nil later), or confusing class (Dog) with instance (d).
Class = blueprint; object = instance (Dog.new). new calls initialize; @vars = per-object state. Everything is an object. Inheritance: Puppy < Dog.
Interview-ready guide
Interview answer
A class defines the shared behaviour for a kind of object. An object is one instance of that class, with its own state. Two Book objects use the methods defined by Book, but each object keeps a different @title.
Calling Book.new("Clean Code") creates a Book object and normally runs initialize. The initializer stores the starting state. Later, book.label sends the label message to that object, and self inside the method refers to book.
In this complete example, both objects share the same label method while their @title values remain separate. attr_reader allows outside code to read the title without giving it unrestricted write access.
Public methods should describe what an object can do, not only expose its storage. A method such as account.withdraw(20) can validate the change and preserve the object's rules. A class method such as Book.new belongs to the class, while an instance method such as label needs a particular object.
Classes and Objects
Direct Answer
A Ruby class defines behaviour and the shape of an object's state; an object is one instance of that class. Calling Book.new(...) creates an object and normally runs its initialize method. Instance variables such as @title belong to that object, while instance methods operate with that object as self.
Deep Dive & Architecture
Book.new is a class-method call. The normal creation path allocates storage for a Book instance and invokes its private initialize method with the supplied arguments. initialize configures the new object's starting state; its own return value is not used as the result of .new, which returns the created object.
An instance variable is attached to one object, not declared once for the whole class. If first and second are different Book instances, assigning first's @title does not change second. An unread instance variable evaluates as nil, which is convenient but can hide a missed initialization unless the constructor establishes required values.
A method call chooses behaviour through the receiver's class and ancestor chain. In book.label, book is the receiver and becomes self while the selected method runs. Local variables disappear with the method call; instance variables remain with the object.
The public surface should describe what the object can do, not merely expose storage. account.withdraw(20) can reject a negative balance, log the operation, and preserve an invariant. Letting callers write account.balance = -20 would make all of those rules optional.
Code Example
class Book
attr_reader :title
def initialize(title)
@title = title
end
def label
"Book: #{@title}"
end
end
first = Book.new('Clean Code')
second = Book.new('Refactoring')
first.label # => 'Book: Clean Code'
second.label # => 'Book: Refactoring'
first.class # => Book
Book.class # => Class
Common Mistakes & Pitfalls
Forgetting to set @vars in initialize (nil later), or confusing class (Dog) with instance (d).
Check your understanding
Answer these without looking back at the article.
- When would you use Classes And Objects in a real project?
- How would you compare Classes And Objects with an alternative?
- What is a common mistake when using Classes And Objects?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.