From: dblack@... Date: 2006-07-14T09:35:58+09:00 Subject: Re: Initialize a variable in an extended class Hi -- On Fri, 14 Jul 2006, Eric Armstrong wrote: > Note: For a long time it wasn't clear to me that > it is impossible to initialize an instance variable > outside of a method. Coming from the Java world, I > just assumed that this would be legal: > class Foo > @bar = 2 > end > > But it isn't. (A fact that needs to be spelled out > somewhere, for Java types.) I'm not sure what you mean by "somewhere". It gets talked about and explained regularly here, as well as in various books, wikis, etc. The workings of instance variables are not a closely guarded secret :-) When you see this: @var you are seeing an instance variable belonging to whatever object is, at that point, playing the role of "self" (the default object). At the top level of a class definition block, "self" is the class object (Foo in your example). So in your example, you've got an instance variable belonging to the class object Foo. There's no connection at all between this instance variable and any instance variable that may come into being later when some other object is "self" (including an instance of Foo). David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS (reviewed on Slashdot, 7/12/2006!) http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log dblack@wobblini.net => me