# Using self.attribute for writing vs reading - confusion

**URL:** <https://www.ruby-forum.com/t/using-self-attribute-for-writing-vs-reading-confusion/91892>\
**Category:** Ruby\
**Created:** [April 13, 2007, 4:28am UTC](https://www.ruby-forum.com/t/using-self-attribute-for-writing-vs-reading-confusion/91892 "2007-04-13T04:28:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 13, 2007, 4:28am UTC](https://www.ruby-forum.com/t/using-self-attribute-for-writing-vs-reading-confusion/91892/1 "2007-04-13T04:28:04Z")

</div>

Lets take this simple example

class NewBook

attr\_accessor :books

def initialize  
@books = []  
end

def some\_method  
books[0] #( the reader for books works fine and will recall the  
class value @books, however)

```
  books = [4,5,6,7] # ( books will not actually get assigned here

```

unless I do self.books = [4,5,6,7] )  
end

end

Why do you have to use self.books= on writer methods, but you can simply  
put books on reader method.

Very confused. Any help would be great.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 13, 2007, 4:56am UTC](https://www.ruby-forum.com/t/using-self-attribute-for-writing-vs-reading-confusion/91892/2 "2007-04-13T04:56:40Z")

</div>

On Apr 12, 10:28 pm, Aryk G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def some\_method  
> put books on reader method.
> 
> Very confused. Any help would be great.
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

In the case of “books = foo”, ruby thinks books is a new locally  
scoped variable. It’s a ruby language “gotcha”. To inform ruby that  
you want to call the “book=” method on your object, you have to  
explicitly mention the reciever: “self.book = foo”

OR, as long as you are in an instance method of that class, you can  
just access the instance variable directly: “@book = foo”

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 13, 2007, 4:57am UTC](https://www.ruby-forum.com/t/using-self-attribute-for-writing-vs-reading-confusion/91892/3 "2007-04-13T04:57:40Z")

</div>

On Apr 12, 2007, at 10:28 PM, Aryk G. wrote:

> Why do you have to use self.books= on writer methods, but you can  
> simply  
> put books on reader method.

books = [1,2,3,4]  
list = [5,6,7,8]

Which one is a setter method and which one is an assignment  
to a local variable?

You have to use self.books so that the parser can  
distinguish between a call to the setter method  
and a local variable assignment. This is a side effect of  
their being no need to declare local variables nor a sigil  
for local variables (like @ and $ for instance and global  
variables).

Gary W.
