VOOZH about

URL: https://www.geeksforgeeks.org/ruby/ruby-getters-and-setters-method/

⇱ Ruby getters and setters Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ruby getters and setters Method

Last Updated : 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. Example 1: Simple get method 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 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
  1. attr_reader : This accessor generates the automatic Getter method for the given item.
  2. attr_writer : This accessor generates the automatic Setter method for the given item.
  3. attr_accessor : This accessor generates the automatic Getter & Setter method for the given item.
  • attr_reader: accessor get method Example 3: 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 method Example 4: 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 method Example 5: 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: 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.
Comment
Article Tags:
Article Tags: