Ruby Academy · Lesson

Instance Variables and Methods

Understand how objects store data and interact with methods inside a class.

Lesson 2 of 410 steps

Instance Variables and Methods is a free Ruby Academy lesson on CoddyKit. This is lesson 2 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.

Working with Instance Data

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.

Instance Variables and Methods — illustration 1

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

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_name

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

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

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

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 = 25

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

Congratulations!

You have learned how instance variables and methods work in Ruby.

Next, you will explore inheritance and polymorphism.

Instance Variables and Methods — 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 “Instance Variables and Methods” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Instance Variables and Methods”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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

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