Ruby Academy · Lesson

Classes and Objects

Learn how to define classes, create objects, and instantiate new instances of a class.

Lesson 1 of 410 steps

Classes and Objects is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Object-Oriented Basics in Ruby

Classes and Objects

Ruby is a fully object-oriented language, meaning everything in Ruby is an object.

In this lesson, you will learn how to define classes, create objects, and instantiate new instances of a class.

Classes and Objects — illustration 1

Defining a Class

Classes are defined using the class keyword.

class Person
end

Creating an Object

You can create an instance of a class using the new method.

class Person
end

person1 = Person.new
puts person1.class

Using Initialize Method

The initialize method runs when an object is created.

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

person1 = Person.new('Alice')

Calling Methods on an Object

You can define and call methods inside a class.

class Person
  def say_hello
    puts 'Hello!'
  end
end

person1 = Person.new
person1.say_hello

Instance Variables

Instance variables store object-specific data and start with @.

class Person
  def initialize(name)
    @name = name
  end
  
  def show_name
    puts @name
  end
end

person1 = Person.new('Alice')
person1.show_name

Instance Methods

Instance methods allow objects to interact with their own data.

class Person
  def initialize(name)
    @name = name
  end
  
  def greet
    puts "Hello, my name is #{@name}!"
  end
end

person1 = Person.new('Alice')
person1.greet

Accessing Instance Variables

Use getter and setter methods to access instance variables.

class Person
  attr_accessor :name
end

person1 = Person.new
person1.name = 'Alice'
puts person1.name

Congratulations!

You have learned how to define classes and create objects in Ruby.

Next, you will explore instance variables and methods in more detail.

Classes and Objects — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Classes and Objects” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Classes and Objects”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Classes and Objects”?

Learn how to define classes, create objects, and instantiate new instances of a class. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Classes and Objects” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Classes and Objects
  2. Instance Variables and Methods
  3. Inheritance and Polymorphism
  4. Mixins and Modules
← Back to Ruby Academy