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. 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.

Defining a Class
Classes are defined using the class keyword.
class Person
endCreating an Object
You can create an instance of a class using the new method.
class Person
end
person1 = Person.new
puts person1.classUsing 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_helloInstance 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_nameInstance 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.greetAccessing 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.nameCongratulations!
You have learned how to define classes and create objects in Ruby.
Next, you will explore instance variables and methods in more detail.

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
- Classes and Objects
- Instance Variables and Methods
- Inheritance and Polymorphism
- Mixins and Modules