Getter and setter methods

baseball

 

Here is how to create “getter” and “setter” methods inside of your class. Why do you need these methods?

  • Lets start with setting up our class.
class Park
end
  • What are some attributes of parks? For this park we want it to have a swing set and a sports court. (It’s going to be an awesome park!)
  • Next we are going to make those attributes our “instance variables” for our park. This means that every time we create a new instance of Park we can change the value of our instance variables!
class Park
 def initialize(swing_set, sport_courts)
 @swing_set = swing_set
 @sport_courts = sport_courts
 end
end
  • Above we are getting our instance variables set up. The “initialize” method is the very first method that is going to be called when write an instance of the class. When we create an instance of the class this is going to set the values to the instance variables. (I will write out creating an instance of the Park class after we set up all our getter and setter methods!)
class Park
 def initialize(swing_set, sport_courts)
 @swing_set = swing_set
 @sport_courts = sport_courts
 end 

 def swing_set
 @swing_set
 end 

 def swing_set=(swing_set)
 @swing_set
 end 

 def sport_courts
 @sport_courts
 end 

 def sport_courts=(sport_courts)
 @sport_courts
 end 

end
  • That is so much code!! Yes, it is a lot and there is a short hand way to write this out, but today we are going to write it out the long way.
  • An example of a “setter” method is on line 11. Here we see “def” which is how we know that it’s method. Then we have the name of the method which is “swing_set” (you can not have spaces in method names). Next we have an equal sign leading to a set of parenthesis with one argument inside it.
  • Inside our method is the instance variable. We can tell it’s an instance variable because of the @ sign.
  • Then this comes to an end with the keyword “end”.
  • An example of the “getter” method is on line 7.
  • The getter method name is “swing_set“. We named it “swing_set” so we know what instance variable the method will be returning.
  • Now let’s create an instance of Park to see why we need all these methods.

great_park = Park.new("blue swing set", "baseball field")

  • “great_park” is going to be the name of the instance that we are going to create. We are setting it equal to a new instance of the Park class (Park.new). Then we are setting the arguments of the initialize method set to: “blue swing set” and “baseball field”.
  • The initialize method sets the instance variables, but what if we wanted to reset the values of those instance variables? This is where the setter method comes in!
  • The setter method is going to let you set a new value to the instance variable. An example: the instance variable “swing_set” is getting set to “blue swing set” because that is what we have passed into the initialize method when we created the new instance of Park. What if we changed our mind and want a “purple swing set”? You would be able to change the value of the instance variable “swing_set” to “purple swing set”!
  • Now that we have gone ahead and set up all our setter methods we want to “get” access to all the variables. This is where the “getter” method comes in.
  • Example: we want to print to the screen that we would like a “blue swing set”. We would write something like this:

puts great_park.swing_set

  • “great_park” is the name of the instance that we have created and then we are attaching the method “swing_set” to it. Since the swing_set method is just returning the value of the instance variable @swing_set that is how “blue swing set” gets printed to the screen.
  • Now if we wanted to go ahead and print out a purple swing set, you could write this…

puts great_park.swing_set=("purple swing set")

Leave a comment