Ruby getters and setters Method

11 Oct, 2019
In a Ruby class we may want to expose the instance variables (the variables that are defined prefixed by @ symbol) to other classes for encapsulation. Then, in that case, we use the getter and setter methods. these methods allow us to access a class’s instance variable from outside the class. Getter methods are used to get the value of an instance variable while the setter methods are used to set the value of an instance variable of some class.
# Ruby Program of getter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # Classical get method  def website    @website  endend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website
Output :
www.geeksforgeeks.org

In this example, if we don’t define the website method in the class, the puts statement used later (gfg.website) would give us an exception because the @website variable is the class’s instance variable and it should not be accessible outside the class by default.
Example 2: Simple set method
# Ruby Program of setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # Classical get method  def website    @website  end    # Classical set method  def website=(website)    @website = website  endend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website  # Change the instance variable from# Outside the classgfg.website="www.practice.geeksforgeeks.org"puts gfg.website
Output :
www.geeksforgeeks.org
www.practice.geeksforgeeks.org

In this example, if we don’t define the website= method in the class, then we can’t change the value of the class’s instance variable. With the help of this setter method, we reused the same object for multiple websites.
In the above examples, we can be seen that as the class grows we might have many getter and setter methods most of which follow the same format as shown above. To fix these growing lines of code, Ruby provides us with a quick way to generate the getter and setter methods without explicitly writing them as we did in the above examples. These methods are known as accessor methods. Their purpose is the same as that of a getter or setter.
There are three types of accessors in Ruby
attr_reader : This accessor generates the automatic Getter method for the given item.attr_writer : This accessor generates the automatic Setter method for the given item.attr_accessor : This accessor generates the automatic Getter & Setter method for the given item.
attr_reader : This accessor generates the automatic Getter method for the given item.
attr_writer : This accessor generates the automatic Setter method for the given item.
attr_accessor : This accessor generates the automatic Getter & Setter method for the given item.
attr_reader: accessor get methodExample 3:# Ruby Program of accessor getter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # accessor get method  attr_reader :websiteend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.websiteOutput :www.geeksforgeeks.org
Here we have replaced the classical getter method with Ruby’s way to generate the getter method. The output remains the same as the attr_reader has the same function as shown in example 1.
# Ruby Program of accessor getter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # accessor get method  attr_reader :websiteend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website
Output :
www.geeksforgeeks.org

Here we have replaced the classical getter method with Ruby’s way to generate the getter method. The output remains the same as the attr_reader has the same function as shown in example 1.
attr_writer: accessor set methodExample 4:# Ruby Program of accessor getter and setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # accessor get method  attr_reader :website    # accessor set method  attr_writer :websiteend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website  # Change the instance variable from# Outside the classgfg.website="www.practice.geeksforgeeks.org"puts gfg.websiteOutput :www.geeksforgeeks.org
www.practice.geeksforgeeks.org
Here we have replaced the classical getter and setter method with Ruby’s way to generate them.
# Ruby Program of accessor getter and setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # accessor get method  attr_reader :website    # accessor set method  attr_writer :websiteend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website  # Change the instance variable from# Outside the classgfg.website="www.practice.geeksforgeeks.org"puts gfg.website
Output :
www.geeksforgeeks.org
www.practice.geeksforgeeks.org

Here we have replaced the classical getter and setter method with Ruby’s way to generate them.
attr_accessor: accessor get and set methodExample 5:# Ruby Program of accessor getter and setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # accessor get and set method  attr_accessor :websiteend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website  # Change the instance variable from# Outside the classgfg.website="www.practice.geeksforgeeks.org"puts gfg.websiteOutput :www.geeksforgeeks.org
www.practice.geeksforgeeks.org
Here both the accessors are replaced by a single attr_accessor as it functions as both a getter and a setter.
# Ruby Program of accessor getter and setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website)    @website = website  end    # accessor get and set method  attr_accessor :websiteend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org"puts gfg.website  # Change the instance variable from# Outside the classgfg.website="www.practice.geeksforgeeks.org"puts gfg.website
Output :
www.geeksforgeeks.org
www.practice.geeksforgeeks.org

Here both the accessors are replaced by a single attr_accessor as it functions as both a getter and a setter.
Mixed use of the accessors :A class may have many instance variables, based on which we may want to have different access permissions of those instance variables to other classes. Here, we see the mixed use of the accessors in a class.Example 6:# Ruby Program of accessor getter and setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website, id)    @website = website    @id = id  end    # accessor get and set method  attr_accessor :website  attr_reader :idend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org", 12puts gfg.websiteputs gfg.idOutput :www.geeksforgeeks.org
12
Here the id instance variable is only given the get method using attr_reader because we don’t want some other class to change its id once it’s initialized.
# Ruby Program of accessor getter and setter methodclass CSWebsite    # Constructor to initialize  # the class with a name  # instance variable  def initialize(website, id)    @website = website    @id = id  end    # accessor get and set method  attr_accessor :website  attr_reader :idend  # Creating an object of the classgfg = CSWebsite.new "www.geeksforgeeks.org", 12puts gfg.websiteputs gfg.id
Output :
www.geeksforgeeks.org
12

Here the id instance variable is only given the get method using attr_reader because we don’t want some other class to change its id once it’s initialized.
Picked
Ruby-Methods
Ruby
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Make a Custom Array of Hashes in Ruby?
Global Variable in Ruby
Ruby | Enumerator each_with_index function
Ruby | Case Statement
Ruby | Array select() function
Ruby | unless Statement and unless Modifier
Ruby | Data Types
Ruby | Hash delete() function
Ruby For Beginners
Ruby | Array class find_index() operation