From: Hal Fulton Date: 2005-06-14T09:38:10+09:00 Subject: Re: are these the same? Lowell Kirsh wrote: > I'm wondering about the difference between writing something like > > @x = ... > and > self.x = ... > > from within a class. Are they the same thing? Not necessarily the same. @x = ... # assigns an instance variable self.x = ... # invokes a method called x= (which doesn't # exist in your example. Now, having said they're different... if you add attr_accessor :x then they are more the same. Bear in mind that instance variables are accessed from the "outside world" only via methods. And when you say self.x within a class, you are basically accessing it "from the outside" even though you are inside the class (dumb analogy: You're in your house, and you reach outside a window and then reach into the window next to it "from outside"). Also bear in mind that if you (manually) define a method x=, you *could* define it in a stupid or misleading way (just as you *could* give misleading names to all your variables and methods): def x=(val) puts "Ha, fooled you! I'm setting @y to 123!" @y = 123 # and not even using val end Hope this helps... Hal