What are Getter and Setter methods in Ruby

Last updated on December 20, 2021

If you want to either retrieve or set a value of an instance variable from a Ruby class, you need to learn about getters and setters. Learning about attr_accessor is also really useful to help you keep your class clean and tidy.

If you want to learn about Ruby classes, you can read about the basics in this article

What is a getter method?

Getter methods help you retrieve the value of an instance variable. If you don't have a getter method, trying to retrieve a variable is going to result in an error.

ruby
1
class Company
2
def initialize(name)
3
@name = name
4
end
5
end
6
7
company = Company.new("The Bluth Company")
8
9
p company.name
10
# => undefined method `name' for #<Company:0x00007fe25f021b30> (NoMethodError)

Let's include a getter method

ruby
1
class Company
2
# ...
3
4
# Getter
5
def name
6
@name
7
end
8
end
9
10
company = Company.new("The Bluth Company")
11
p company.name
12
# => "The Bluth Company"

What is a setter method?

Setter methods help us assign new values to our instance variables. Let see what happens if you don't have a setter method and you try to assign a new value to your instance variable

ruby
1
class Company
2
def initialize(name)
3
@name = name
4
end
5
6
# Getter
7
def name
8
@name
9
end
10
end
11
12
company = Company.new("The Bluth Company")
13
p company.name
14
# => "The Bluth Company"
15
16
company.name = "Dunder Mifflin"
17
# => undefined method `name=' for #<Company:0x00007fdd680f50f8 @name="The Bluth Company"> (NoMethodError)

So let's create a setter method

ruby
1
class Company
2
# ...
3
4
# Setter
5
def name=(name)
6
@name = name
7
end
8
end
9
10
# ...
11
company.name = "Dunder Mifflin"
12
# => "Dunder Mifflin"

What is a Ruby attribute accessor?

You can see how classes can become quite big with having to define 2 methods for every instance variable. That's why we have access to 3 build-in Ruby methods:

  • attr_reader - generates a getter method
  • attr_writer - generates a setter method
  • attr_accessor - generates both getter and setter methods
ruby
1
class Company
2
attr_reader :name
3
attr_writer :name
4
5
def initialize(name)
6
@name = name
7
end
8
end
9
10
company = Company.new("The Bluth Company")
11
p company.name
12
# => "The Bluth Company"
13
14
company.name = "Dunder Mifflin"
15
p company.name
16
# =>"Dunder Mifflin"

When you want bothattr_readerandattr_writer, just use

ruby
1
attr_accessor :name

You can define multiple attributes on the same line, which will generate the relevant methods

ruby
1
attr_accessor :name, :email, :address

By the way, you can get a list of your instance variables using the built-in#instance_variablesmethod and a list of your class methods using#public_methodsmethod

ruby
1
class User
2
attr_accessor :name
3
attr_accessor :age
4
5
def initialize(name, age)
6
@name = name
7
@age = age
8
end
9
end
10
11
user = User.new("Kris", 24)
12
13
p user.instance_variables
14
# => [:@name, :@age]
15
16
p user.public_methods
17
# => [:age, :age=, :name, :name=]

Combination of attribute accessor

Sometimes you might want to combine your attribute accessor methods depending on your variables. For example, you might want to read and write the email variable but only read the id variable.

ruby
1
attr_accessor :name
2
attr_reader :id

Using attr_accessor in a Rails model

At its core, a Rails model is just a Ruby class. The model attributes are defined using attr_accessor under the hood, but Rails automatically does it for us in order to keep our models tidy.

However, if you want to add an attribute that is not defined in the model's table, you can define it manually using attr_accessor. This will create a non-persistent instance variable.

ruby
1
# models/customer.rb
2
class Customer
3
# ...
4
5
attr_accessor :password_token
6
end

These non-persistent instance variables are useful in cases when you only need the variable for a limited time or you don't need to store it in your database.

Invite us to your inbox.

Articles, guides and interviews about web development and career progression.

Max 1-2x times per month.