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")

Classes in Ruby

coffee

I spent today learning about classes in Ruby! With a little bit of coffee and a lot of reading I feel that I am starting to understand more about classes in Ruby. What is a class and why are they useful in Ruby? Here is a little bit of information on how to create a class:

heading_blog_shapes

 

I have all these shapes and I want them to be in the same class. So let’s make “Shape” class:

shape_class1

 

We created a class by writing “class” and then the name of the class which is “Shape”. Let’s “initialize” our Shape class so that it will do something.

shapes_class2

Now we added a method inside of our Shape class. The method has a very special name though…“initialize” can be thought of like an “on switch” for your class. Then you see two arguments for the method, which are “name, color”. Inside our method you will see “@name” and “@color” again, but why? Good question. These are know as “INSTANCE VARIABLES”. Let’s keep going and see why we need instance variables.

What’s going on now?! There is another method inside our Shape class. We added a method called “description” and notice that it has those “INSTANCE VARIABLES” again. We are going to use those variables from that first method and use it another method…and we are allowed to do that because they are special kinds of variables. You can tell that these are special variables, because they have the @ in front of them. OK, we have all this information, now what? Let’s “initialize” an instance of this class! (Get use to those words…initialize and instance.)

shape_class4

More code! OK…now we are creating instances of the class “Shape” and assigning them to variables. For example “square” is an instance of “Shape”. Now this is all great, but we aren’t printing anything to the screen yet. Our “puts” is tucked away inside that “description” method. How will anyone see all this awesome stuff?! Let’s add the last piece to this class puzzle.

shapes_class5

 

And now you will have the following printed out!