Mike Henze
Sep 3, 2018 · 1 min read

Getter & Setter Methods in Ruby

If you would have asked me this question 1 week ago, what is a getter and setter method in ruby? I would have looked as puzzled as the person asking the question. Now being 1 week into my programming bootcamp Actualize I have to say the amount of knowledge I have learned so far is astounding.

Now to go over the question what is a Getter and Setter Method and why should you care? in Ruby and other programming languages you are developing applications that have a class with functions that perform multiple actions, now to access these functions in a class you must have a way to get and set the values inside the functions. That is where a getter and setter method comes in and there are other ways of creating a getter and setter method but this is a very simple method and does not require writing a large amount of code.

In conclusion to access and use this method all you have to do is add them to the top of your ruby document by writing attr_reader :name :age to access the getter method and using attr_writer :name :age to access the setter method. There is an even easier way by accessing the getter and setter method with one line of code at the top of the class method which is attr_accessor :name :age.