DEV Community

Jonathan_Cummings
Jonathan_Cummings

Posted on

Attribute accessors what are they?

In this blog post I will be referencing my phase 1 project at Flatiron school. At Flatiron I am taking the online software engineering boot camp! In this blog I will be discussing my first coding language which is the coding language of Ruby! The project I worked on was a CLI application connected to an API. This CLI application allows you access to a list Studio Ghibli movies as well as giving you pertinent information on the films. Once I got started on this project I could not stop, it's so much fun creating CLI applications. It was very enjoyable to code away at and it helped strengthen my skills in Ruby which really enhanced my understanding. I recommend building a CLI application.

At first I struggled with understanding the concept of attribute accessors. What is this, how can I use it, where do I use it? I had questions and I wanted answer. When building a class it is very important to really understand and grasp these concepts. I hope in this you will have a better understanding. This will also save you time as it did me.
I will be giving a brief overview of what attribute accessors are and how you can use them.

Ok let's dive in first we have these three:
attr_reader

attr_writer

attr_accessor

Using these we can access a class's instance variable from outside of the class, which is very imporant for added "attributes." attr_reader & attr_writer are "getter" and "setter" methods. So attr_reader will generate the "getter" method for the item needed. attr_writer will generate the "setter" method for the item needed. attr_accessor however can do both "getter" & "setter"! You can see how imporatnt these are and how versatile attr_accessor is and how it'll save you some lines of code, which is very always my favorite. The cleaner more to the point the better!
For the sake of showing examples I will be simplifying my code
Examples:
First attr_reader getter

class Movies
    attr_reader :title

    def initialize(title)
        @title = title
    end
end
Enter fullscreen mode Exit fullscreen mode

with attr_reader we are able to "get" the name and create it
Second attr_writer setter

class Movies
    attr_writer :title

    def initialize(title)
        @title = title
    end
end
Enter fullscreen mode Exit fullscreen mode

with attr_writer we are able to change the title
third attr_accessor getter + setter
my favorite

class People
    attr_accessor :name

   def initialize(title)
        @title = title
    end
end
Enter fullscreen mode Exit fullscreen mode

with attr_accessor we are able to both create it as well as change it!

I prefer to use attr_accessor because it does two jobs in one but be mindful of what you're asking it to do. Sometimes you don't want you attribute to be changed, you want it to stay the same, would it be a good idea to use attr_accessor? I don't think so, therefore a getter method is preferred. This can be worked in reverse of course if there was a need for only a setter method not a getter method you'd only use attr_writer verus attr_accessor. It's important to look at your code and think what exactly do I want this to do? Every time I'm working on a lab or coding independently I always ask myself that so I can better complete the code.

resources for further reading that I recommend
https://www.geeksforgeeks.org/ruby-getters-and-setters-method/
https://www.rubyguides.com/2018/11/attr_accessor/
I encourage to you continue reading and learning!
I hope what you read will help and give you a little bit more insight. To whoever is reading this I hope you have a wonderful day!

Top comments (0)