0 points
Submitted by Daniel Golden
over 9 years

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

105 votes

Permalink

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
points
Submitted by Kulja
over 9 years

13 comments

John Magee over 9 years

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

Daniel Golden over 9 years

Hey, thanks for explaining that, Kulja. I appreciate it!

Maya Kherani over 9 years

A much clearer explanation - thanks!

Dangrous over 9 years

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…

raffaele almost 9 years

very good explanation!!!!

Sam Clark almost 9 years

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.

SZultimatecoder over 8 years

thx

Dylan Pool over 8 years

Very Good, Thanks! :D

Abu-Bakr almost 8 years

THUMBS UP!!!

Alex W over 7 years

Thank you!

Jonathan Gall over 7 years

Fantastic explanation, thank you!

Akeem about 7 years

I didn’t really understand until I read this! THANK YOU!!

Michael Du almost 7 years

you’re amazing. please have a beer on me.

Answer 50f1cf0be333c96c5500001b

3 votes

Permalink

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.

points
Submitted by John Magee
over 9 years

Answer 51ae03eeca96059478003946

2 votes

Permalink

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.

points
Submitted by Jonathan Toms
about 9 years

1 comments

Boris Atanasov about 7 years

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

-4 votes

Permalink

OK so if I get this right all instance variables in Ruby are private by default and there is no public modifier for variables ?? The only way to make an instance variable accessible outside of the instance is by public method ?

points
Submitted by Kedlaw
over 9 years