Ruby Build an Address Book in Ruby Class Design Address Class: Part 2

Ulfar Ellenarson
Ulfar Ellenarson
5,277 Points

Contact.class and the attr_reader :addresses

HI I was wondering about the the attr_reader :addresses in the method def initialize where it is set to an empty array @addresses = [] and my question is I would have thought that a attr_reader is a getter (that is it only displays attributes of the object) but in the method add_address we are adding the attributes from the address.rb to the attribute @addresses which I thought would need to be an attr_writer or attr_accessor. Could someone point me as to how a attr_reader can be changed ie. @addresses going from an empty array to one filled with the object attributes from the Address class.

3 Answers

Ilya Dolgirev
Ilya Dolgirev
35,374 Points

attr_reader, attr_writer and attr_accessor are methods for gaining or restricting reading/writing access to attributes from outside.

Ulfar Ellenarson
Ulfar Ellenarson
5,277 Points

I realised that I was looking at constructors (getters and setters in ruby) in the wrong way. I forgot temporarily that attr_reader enables the object attributes to be read from outside the class/method. In the same way that attr_writer enables the object to be set outside the class/method. I completely forgot about private and public methods in ruby. Thank you for the answer.

Ilya Dolgirev
Ilya Dolgirev
35,374 Points

You're welcome! Good luck to learn Ruby! :)