From: jg.ruby@... Date: 2007-04-13T11:55:13+09:00 Subject: Re: using self.attribute for writing vs reading - confusion On Apr 12, 10:28 pm, Aryk Grosz wrote: > 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. > > -- > 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"