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.
1class Company2def initialize(name)3@name = name4end5end67company = Company.new("The Bluth Company")89p company.name10# => undefined method `name' for #<Company:0x00007fe25f021b30> (NoMethodError)
Let's include a getter method
1class Company2# ...34# Getter5def name6@name7end8end910company = Company.new("The Bluth Company")11p company.name12# => "The Bluth Company"
1class Company2def initialize(name)3@name = name4end56# Getter7def name8@name9end10end1112company = Company.new("The Bluth Company")13p company.name14# => "The Bluth Company"1516company.name = "Dunder Mifflin"17# => undefined method `name=' for #<Company:0x00007fdd680f50f8 @name="The Bluth Company"> (NoMethodError)
So let's create a setter method
1class Company2# ...34# Setter5def name=(name)6@name = name7end8end910# ...11company.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
1class Company2attr_reader :name3attr_writer :name45def initialize(name)6@name = name7end8end910company = Company.new("The Bluth Company")11p company.name12# => "The Bluth Company"1314company.name = "Dunder Mifflin"15p company.name16# =>"Dunder Mifflin"
When you want bothattr_readerandattr_writer, just use
1attr_accessor :name
You can define multiple attributes on the same line, which will generate the relevant methods
1attr_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
1class User2attr_accessor :name3attr_accessor :age45def initialize(name, age)6@name = name7@age = age8end9end1011user = User.new("Kris", 24)1213p user.instance_variables14# => [:@name, :@age]1516p user.public_methods17# => [:age, :age=, :name, :name=]
1attr_accessor :name2attr_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.
1# models/customer.rb2class Customer3# ...45attr_accessor :password_token6end
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.
Level up your web development skills
Get articles, guides and interviews right in your inbox. Join a community of fellow developers.
No spam. Unsubscribe at any time.