How to use Getters and Setters in Ruby

Shaqqour
2 min readJan 15, 2021

--

Photo by Caspar Camille Rubin on Unsplash

Getter and Setter methods in Ruby are gateways to access class instance variables. In this blog post I will explain how to create a class with getter and setter methods.

Let’s explain what a getter and a setter method in Ruby are!

A Getter method is the only way where you can retrieve the value of an instance variable from outside its class. A Setter method is the only way where you can assign a value to an instance variable from outside its class.

Now that we have an idea on what each one is, let’s explain them using an example.

Here is a Student class:

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

student = Student.new('Sarah')
p student.name #=> undefined method `name' (NoMethodError)

We defined a Student class and then later on we created a student giving her the name “Sarah”, however when we tried to access the student name, it gave us a NoMethodError because it is trying to look for a method called name but we never defined this method.

As we said before, we need to define a getter method to be able to retrieve the student name:

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

#getter method
def name
@name
end
end

student = Student.new('Sarah')
p student.name #=> "Sarah"

Here we were able to retrieve the name because we defined a getter method for the name instance variable.

Now, let’s try to change the name of the student:

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

#getter method
def name
@name
end
end

student = Student.new('Sarah')
p student.name #=> "Sarah"
student.name = 'Pumpkin' #=> undefined method `name='(NoMethodError)

Here when we tried to assign another name to the student, we got a NoMethodError because we never defined a setter method for the name instance variable.

Here is how we define a setter method:

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

#getter method
def name
@name
end

#setter method
def name=(name)
@name = name
end
end

student = Student.new('Sarah')
p student.name #=> "Sarah"
student.name = 'Pumpkin'
p student.name #=> "Pumpkin"

Notice how we defined the setter method adding = to the end of its name to indicate it is a setter method. When we do student.name = 'Pumpkin' this will automatically call the setter method sending whatever is after the equal sign = to the method as a parameter.

Hope that gave you a clear explanation on Getters and Setters in Ruby. If you would like to add anything, please comment below!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shaqqour
Shaqqour

Written by Shaqqour

Full stack software engineer. Passionate about making people’s lives better and easier through programming. LinkedIn.com/in/shaqqour

No responses yet

Write a response