Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,375 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,375 Points

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