Instance Variables and Methods
Understand how objects store data and interact with methods inside a class.
Instance Variables and Methods is a free Ruby Academy lesson on CoddyKit — lesson 2 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
Instance Variables and Methods
Instance variables store object-specific data, and instance methods allow objects to interact with their data.
In this lesson, you will learn how instance variables and methods work in Ruby classes.

2
What are Instance Variables?
Instance variables store data specific to an object and start with @.
class Person
def initialize(name)
@name = name
end
end
person1 = Person.new('Alice')3
Accessing Instance Variables
Instance variables can be accessed using instance methods.
class Person
def initialize(name)
@name = name
end
def show_name
puts @name
end
end
person1 = Person.new('Alice')
person1.show_name4
Getter Method
Getter methods allow reading an instance variable from outside the class.
class Person
def initialize(name)
@name = name
end
def name
@name
end
end
person1 = Person.new('Alice')
puts person1.name5
Setter Method
Setter methods allow modifying an instance variable from outside the class.
class Person
def initialize(name)
@name = name
end
def name=(new_name)
@name = new_name
end
end
person1 = Person.new('Alice')
person1.name = 'Bob'
puts person1.name6
Using attr_accessor
attr_accessor automatically creates getter and setter methods.
class Person
attr_accessor :name
end
person1 = Person.new
person1.name = 'Alice'
puts person1.name7
Using attr_reader and attr_writer
attr_reader creates a getter, while attr_writer creates a setter.
class Person
attr_reader :name
attr_writer :age
end
person1 = Person.new
person1.age = 258
9
Defining Instance Methods
Instance methods allow objects to perform actions using their instance variables.
class Person
def initialize(name)
@name = name
end
def greet
puts "Hello, my name is #{@name}!"
end
end
person1 = Person.new('Alice')
person1.greet10
Congratulations!
You have learned how instance variables and methods work in Ruby.
Next, you will explore inheritance and polymorphism.

Frequently asked questions
Is the “Instance Variables and Methods” lesson free?
Yes — the full text of “Instance Variables and Methods” 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 “Instance Variables and Methods”?
Understand how objects store data and interact with methods inside 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Instance Variables and Methods” 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