Ruby Getters & Setters Methods

Linda Ramos
5 min readOct 13, 2020

I’ve worked with a few projects with Ruby on Rails, so I thought it would be a good idea to return to the fundamentals of just Ruby. I decided to revisit getter and setter methods. These methods help with retrieving values or creating values for an instance variable. This blog will describe what getters and setters are, their syntax, and an example of how to use them. It’s also assumed that you have an understanding of what a Ruby classes and instances are.

A method that is retrieving a value is getting a value, hence the name getter method. The getter method will display information, but it cannot change, edit, or update anything. A method that is changing or updating a value is setting a value, hence the name setter method. The setter method will not display anything, but can change, edit or update an instance variable’s value.

In order to get or set something, we need to know what what is being get or set. The initialize method will help with that.

The example model for this blog will be creating very basic information for a student. The only concern is a student’s name and major. We start off with a class of Student. Any student instance created from this class will be initialized with a name and a major, as seen in Image A below.

Image A

Lines 1 through 6 represent the student class with the initialize method taking the parameters of name and major. Line 8 represents the creation of a new instance a student being created with the name of “John” and major of “Math”. Line 9 will putsout the instance created.

Run the code using ruby student.rb. Image B is the result of the puts statement from line 9. Notice the instance is represented as #<Student>:0x00007ffab989a6f0>. We do not see the the name or major information.

Image B
Image C

If we would like to know the name of that instance, we can try student.name.

Line 9 was updated in attempt to get the name of the instance using student.name. An error followed as shown in Image D

Image D

The error message in Ruby says undefined method 'name' for .... Notice that the instance does show @name = "John" and @major = "Math". This does confirm that our instance has the values we entered, but we need the method to get the values. Since Ruby didn’t find a method called name to get the name, let’s make one.

Image E

The method def name has now been added. (Note: The method name can be whatever you like, but it should reflect the name should reflect the purpose of the method. get_name is a more explicit/better method name.)When the method is called using student.name, Ruby will use the method def name, then see the @name instance variable. The information acquired is@name = "John" as was show in image D. Ruby will share the value of the instance variable which is just "John". Running the command ruby student.rb will show the result of the student’s name being sought after as in Image F.

Image F

If we try to get the student’s major using student.major that won’t work since we have not set up a getter method to get the information. Let’s update the code to do exactly that. I encourage you to try it yourself before looking at the example.

Image G

The student class now has two getter methods: def name and def major. When these methods are called in line 19 and 20 in Image G, the result will be puts out when running the command ruby student.rb once again.

Image H

Image H shows the result of getting the student’s name and major.

Now let’s say that the student has decided to change their major. All that is possible is getting the major information. There is no way to update, or set the major. This is where a setter method comes in handy.

Image I

The method name set_major can be whatever you like, but it makes sense to title the method reflecting what it will do. The =(major) is saying that whatever is being passed into the parameter (major), will be set equal to something shortly. Line 19 shows that whatever was passed into the parameter of (major) will now be set to the instance variable @major. This is what allows us to take in a new major and update the value of the instance variable.

Image J

The code thus far is in Image J. We have an initialize method, a getter method called name, a getter method called major, and a setter method called set_major.

The puts statements are in order to see the original major that was initialized and the updated major using set_major.

Image K below shows the results of the puts statements.

Image K

Notice the original major was “Math”. Then theset_major method was used in line 28 of Image J to update or set the major to “Biology”. The the major method was called again to puts the update value as seen in Image K.

Those are the basics of setter and getter methods. It does take some time to understand them. I encourage you to make your own example to make setter and getter methods for. Make sure to step away from your code and return to it in a week or two to test your knowledge of what you learned.

--

--