Monday, March 18, 2013

Getters and Setters


Here are some basic notes on getters and setters. This is my first  experience with OOP and I will try to no longer write procedural based methods and move completely over to OOP in the upcoming weeks for all exercises in order to grasp this concept in preparation for RSpec starting next week. 
But what is happening behind the scenes?
If you write:
attr_writer :age
That gets translated into:
def age=(value)
  @age = value
end
If you write:
attr_reader :age
That gets translated into:
def age
  @age
end
If you write:
attr_accessor :age
That gets translated into:
def age=(value)
  @age = value
end

def age
  @age
end

No comments:

Post a Comment