Ruby Programming – How to generate Getter and Setter methods automatically?

In our previous Article, we have discussed about Getter and Setter methods in Ruby. We no need to add these methods explicitly; instead, Ruby provides a way to Automatically generate these methods to us.

In this Article, we will discuss about generating Getter and Setter methods Automatically in Ruby.

The attr_ methods

Ruby provides keywords, we call them as attr_ methods to generate these Getter and Setter methods at run-time. We have 3 variations of these methods and would like to discuss each one of them through below sections.

The attr_accessor Keyword

By applying this keyword to the variables of the class, Ruby automatically creates a set of Getter and Setter methods to allow to access these variables from outside of the class. For example, below is the statement to automatically creates Getter and Setter methods for the variable var.

attr_accessor :var

Observe that, a colon (“:”) is required before the variable and the variable must be defined using the keyword attr_accessor. This instructs Ruby to generate the methods to access this variable from outside of the class.

But how about restricting to modify the variables of the class? That means, allow to access the variables but restrict to modify them.

If we use this keyword; by default Ruby generates Getter and Setter methods. To restrict to modify the variables; you can use the attr_reader method.

The attr_reader Keyword

This is used to generate the Getter methods for the variables of the Class. By using this keyword, Ruby doesn’t creates the Setter method; hence you are NOT ALLOWED to modify the variables of the Class, from outside of it.

attr_reader :var

If we have reader method; then don’t we have writer method? Yes. We have it. Ruby provides attr_write method to create the Setter methods for variables of the Class.

The attr_writer Keyword

This method is used to generate the Setter methods for the variables of the Class.

By using it, Ruby generates ONLY the Setter methods. That means, you are allowed to modify the values of variables of the Class.

attr_write :var

Remember that, you are NOT allowed to access the variable if you use ONLY this method. If you attempt to access the variable, Ruby throws below Error.

var: undefined method `var' for #<Sample:0x9bc>

Then how to resolve this? It is Simple. You can use attr_reader method to allow to access the variables. That means, by combing these two methods; you can generate both Getter and Setter methods for the variables of the Class. This is what attr_accessor do it for us.

Does these can be used for both Instance and Class variables?

Yes. We can use these methods for both Instance and Class variables. But there is a little bit modification required for Class variables. You must use these methods with in the class << self group. For example,

class << self
	attr_accessor :var
end

To understand much better here is the complete working example.

class Sample
  @instance_var
  @read_only_var
  
  def initialize
    @instance_var = "Instance Variable"
    @read_only_var = "Ready only Variable"
  end
  
  class << self
    attr_accessor :cls_var
  end
  
  attr_accessor :instance_var
  
  attr_reader :read_only_var
end

Sample.cls_var = 100
puts Sample.cls_var

obj = Sample.new
puts obj.instance_var
obj.instance_var = "Modified Text"
puts obj.instance_var

puts obj.read_only_var
#obj.read_only_var = "You can't modify it"

Observe that, the above class has two instance variables and one class variable defined. One instance variable is read only; and if you attempt to modify it; Ruby will throw the below message.

read_only_var=: undefined method `read_only_var=' for #<Sample:0x4ff8>

And also observe that, how the attr_ methods are used for Class variables.

| Nick |

Add a Comment

Your email address will not be published.