From: "Gennady F. Bystritsky" Date: 2002-11-15T02:00:16+09:00 Subject: Re: Another Newbie question regarding instance variables? Cool. Never thought of it this way. And what's the buzz in another thread about making them private? Does it mean that as it currently implemented you can access such variables even directly without accessor methods? Gennady. ----- Original Message ----- From: To: "ruby-talk ML" Sent: Thursday, November 14, 2002 8:32 AM Subject: Re: Another Newbie question regarding instance variables? > Hi -- > > On Fri, 15 Nov 2002, Gennady F. Bystritsky wrote: > > > I thought that I knew Ruby pretty well until I came accross this thread > > about "class instance variables". It always haunted me why it does not work > > when I do > > > > @var = value > > > > outside any method definition, meaning initialization of an instance > > variable. Now I see why. Hence the question: is there any practical use for > > "class instance variables"? If so, would anybody post an example? > > > One potentially good thing, though I can't claim to have any dazzling > examples, is that a class's instance variables can be used as true > class-scoped variables, as opposed to class variables which are sort > of tree-scoped. > > For example: > > class A > class << self; attr_accessor :var; end > @var = 0 > def increment > self.class.var += 1 > puts "New value: #{self.class.var}" > end > end > > a = A.new > a.increment # New value: 1 > b = A.new > b.increment # New value: 2 > > class B < A > @var = 100 > end > > c = B.new > c.increment # New value: 101 > b.increment # New value: 3 > > > David > > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > > >