0Pricing
Ruby Academy · Lesson

Classes and Objects

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

Classes and Objects is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

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

2

Defining a Class

Classes are defined using the class keyword.

class Person
end

3

Creating an Object

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

class Person
end

person1 = Person.new
puts person1.class

4

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')

5

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

6

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

7

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

8

9

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

10

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

Frequently asked questions

Is the “Classes and Objects” lesson free?

Yes — the full text of “Classes and Objects” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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