In other words, what are attr_reader & attr_writer for?
I read through the exercise description and explanation on the left, but I’m a lot confused. I understand what attr_reader
and attr_writer
do, but not what they’re for.
Why would I need attr_reader
& attr_accessor
to do what they do?
Answer 50f29a84506546db33004a8f
Well, attr_reader
and attr_writer
are connected in some way to private
and public
.
You use private
and public
for methods. So with private
and public
you can make methods accessible or not outside of the class.
With instance variables, you can make them accessible or not with attr_reader
and attr_writer
outside of the class.
Let’s use this example:
class Person
def initialize(name)
@name = name
end
end
Let’s say you create one Person
p1 = Person.new("John")
With the above class there is no way to change the name John to anything else. You would need to create another Person object with different name. Sometimes that is what you want and by not using attr_reader
and attr_writer
you are achieving that.
Now let’s look at this:
class Person
attr_reader :name
attr_writer :name
def initialize(name)
@name = name
end
end
Now we can change the name of our Person (p1.name = "Other Name"
) without creating another Person object.
OR, if you want you can use methods for reaching instance variables instead of attr_reader
and attr_writer
but that is not in Rubys style since it is more code
Example:
class Person
def initialize(name)
@name = name
end
def name
@name
end
def name=(value)
@name = value
end
end
13 comments
I get it now, maybe the course could go into more detail to explain that, as I understand this explanation but not so much from the original course…Thanks
Hey, thanks for explaining that, Kulja. I appreciate it!
A much clearer explanation - thanks!
So, I get attr_writer, because of this (which is super helpful, thanks!) but I still don’t get attr_reader. If I do Person.name, don’t I still get the name, even without attr_reader :name? I just tested this and it seems to work…
very good explanation!!!!
Very nice explanation, that really helped. @Dangrous: if I’m understanding it correctly, the value of the attr_reader :name is so that the name can be accessed, but not changed. attr_writer provides the function to change an object’s attribute, while attr_reader allows for us to simply access it – for example, if we need it as the input for another method.
thx
Very Good, Thanks! :D
THUMBS UP!!!
Thank you!
Fantastic explanation, thank you!
I didn’t really understand until I read this! THANK YOU!!
you’re amazing. please have a beer on me.
Answer 50f1cf0be333c96c5500001b
Yes please, I was coming here for the same thing. The section is about information hiding, and then suddenly you move over to the read/write/access ideas using symbols with no clear reason or explanation how it connects to the public and private methods that were being covered previously.
Answer 51ae03eeca96059478003946
My confusion was caused because I created a instance of the class and tried to print and change the attributes, and I was able to do it successfully even with attr_reader and attr_writer commented out, and with no methods. I couldn’t understand why we needed them at all. But when I opened my text editor and pasted the code and ran it using Ruby on my own computer, it didn’t work without attr_reader and attr_writer. So I think there is a problem with Codecademy’s implementation here.
1 comments
The system in Codeacademy is just made to give some more space to us (learners) in the beginning and even after. For that reason some times it pass the code which is not 100% correct.
Answer 51672201880ad21d82000563
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons