From: dblack@... Date: 2007-08-17T09:06:37+09:00 Subject: Re: Does singleton variables have any meaning ? Hi -- On Fri, 17 Aug 2007, Arno J. wrote: >>> class << self #or class << A >>> >>> @singleton_variable # <- Can this be used/called elsewhere ? >> >> Like all instance variables, it belongs to whatever object is 'self' >> at the point where the instance variable appears, and multiple >> references to the same instance variable in different contexts where >> self is the same, will be references to the same instance variable. > > But here self is #. Here it really the same context/self as for > class instance variables (A). self is # (the singleton class of A), so @singleton_variable belongs to #. It has no connection to A. > >> I don't find the term "singleton variable" very helpful. All the >> variables in your example are just instance variables. Calling them >> "class instance variables" is sometimes useful, since there are > > > So, let's take an another example. After every "puts self" I put the > result. > > class A > puts "Inside class" > puts self # A -> This is the self that our instance variable would be > linked to, making a "class instance variable" > > def meth > puts "Inside method" > puts self # # > end > > def self.methc > puts "Inside class method" > puts self # A > end > > class << self > puts "Inside singleton" > puts self # # -> and this is the self that our instance > variable would be linked to... > > def meths > puts "Inside singleton method" > puts self # A > end > end > end > > a = A.new > # > > With a being #, the results are : > - A > - # > - # > > So, when defining what's usually called a "class instance variable", the > self is "A". For what I call a "singleton variable", but that you called > a "class instance variable", the self is "#". > To me, there's a difference, but I don't know what that "#" is. # is the singleton class (sometimes called the "metaclass") of A. It's a separate object; it has its own instance variables, and doesn't share them with A. As for the terminology; here: class << A @var end @var is an instance variable, and self is a class. So we can call it an instance variable or a class instance variable. I don't think it's a good idea to introduce yet another term (singleton variable) for it; it just gets too confusing. > It's the same thing with a self.method. > Can someone tell me how to use those @my_variable and self.method in the > following class : > > class A > class << self > @my_variable > def self.my_method > end > end > # This is where I'd like to use it > end You can't use it there, because you're in a new 'self' context (A rather than #). Therefore the instance variables belong to a different object. You could get indirect access to it if the object that owns it creates wrapper methods for it. But if you put @my_variable where your "This" comment is, it will be a completely different @my_variable. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)