Classes & Objects

Ruby is a dynamic, beginner-friendly programming language built around objects, and a class is the blueprint you use to model real-world things — bundling data and the behaviour that goes with it.

Learn Classes & Objects in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll define classes, create objects, store data in instance variables, and write instance and class methods.

What You'll Learn in This Lesson

1️⃣ Defining a Class and Creating Objects

Use class Name ... end to define a blueprint. The special initialize method runs when you call .new , and stores incoming data in instance variables (the @ ones). Instance methods then act on that data.

2️⃣ Class Variables, Class Methods, and to_s

A class variable ( @@ ) is shared across all instances — perfect for a running total. A class method (defined with self. ) is called on the class itself. And defining to_s controls how your object appears when you puts it.

Your turn. Flesh out a Book class with a summary and a long? predicate. Complete the TODO s, then run it.

Build a small Counter with increment and reset methods that manage an instance variable. Run with ruby counter.rb .

📋 Quick Reference — Classes & Objects

Practice quiz

Which method runs automatically when you call Dog.new(...)?

  • new
  • setup
  • initialize
  • create

Answer: initialize. Ruby calls initialize automatically when you create an object with .new.

What does a single @ prefix mark, as in @name?

  • An instance variable, unique to one object
  • A class variable shared by all instances
  • A global variable
  • A constant

Answer: An instance variable, unique to one object. A single @ marks an instance variable that belongs to one specific object.

What does @@ mark, as in @@total_accounts?

  • An instance variable
  • A local variable
  • A frozen value
  • A class variable shared by ALL instances of the class

Answer: A class variable shared by ALL instances of the class. @@ marks a class variable, shared across every instance of the class.

How is a class method defined?

  • def @method_name
  • def self.method_name
  • class def method_name
  • static def method_name

Answer: def self.method_name. A method defined with self. (def self.total) is a class method, called on the class itself.

For rex = Dog.new("Rex", "Lab"), what does rex.class return?

  • Dog
  • "Rex"
  • Object
  • Class

Answer: Dog. rex.class returns the class the object was made from: Dog.

Defining to_s on a class controls:

  • How the object is sorted
  • Whether the object is frozen
  • How the object prints with puts and string interpolation
  • The object's class name

Answer: How the object prints with puts and string interpolation. to_s defines the object's string form, which puts and interpolation use.

If you write a plain name = value (no @) inside an instance method, what happens?

  • It sets the instance variable @name
  • It creates a local variable that vanishes when the method ends
  • It raises a NoMethodError
  • It defines a class variable

Answer: It creates a local variable that vanishes when the method ends. Without @, name is just a local variable that disappears; use @name to persist data on the object.

How do you call a class method named total_accounts on BankAccount?

  • BankAccount.new.total_accounts
  • total_accounts(BankAccount)
  • @@total_accounts
  • BankAccount.total_accounts

Answer: BankAccount.total_accounts. Class methods are called on the class itself: BankAccount.total_accounts.

By convention, a boolean (predicate) method like 'is it long?' should be named:

  • is_long
  • long?
  • long!
  • get_long

Answer: long?. Ruby names predicate methods with a trailing ?, like long? or active?.

Why can't you read an object's @name directly from outside the object?

  • @name does not exist until printed
  • Ruby forbids the @ symbol outside classes
  • Instance variables are private; you expose them via methods (e.g. attr_accessor)
  • You can — there is no restriction

Answer: Instance variables are private; you expose them via methods (e.g. attr_accessor). Instance variables are private to the object; getters/setters (often attr_accessor) expose them.