Tuesday, March 8, 2011

reader and writer / getters and setters methods in ruby on rails

class Car
# A writer method. Sets the value of the @make attribute
def make=(text)
@make=text
end

# A reader method. Returns the value of the @make attribute
def make
@make
end
end
==================
#create object
my_car=Car.new

#read object
my_car.make #=> nil

#write
my_car.make='Toyota'

#read
my_car.make #=> 'Toyota'
--------------
#write
my_car.make='Mazda'

#read
my_car.make #=> 'Mazda