Hi --
unknown wrote:
self is #<Class:A> (the singleton class of A), so @singleton_variable
belongs to #<Class:A>. It has no connection to A.
Ok, that's what I guessed
#<Class:A> 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:
@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.
I'm sorry but I still don't get it (maybe I'm a little bit nit_picking
also)
class A
@first_instance_var # self = A
def meth
@second_instance_var # self = #<A:0x2ebd140>
end
class << self
@thir_instance_var # self = #<Class:A>
end
end
All three variables are instance variables (just because of the @, as
far as I know).
BUT, for what I've read on the web / in books
first_... would be called a "class instance variable"
second_... would be called an "instance variable"
third_... would not be called (I never met any), or would be called a
"class instance variable" according to you, although self is not a class
but a singleton class.
Singleton classes are classes. More importantly, though, it's best to
keep the terminology as simple as possible.
All of these things are instance variables. The only reason to use a
longer name is to make it clear in usage where someone might not know
what you mean. If you've got an instance variable of #<Class:A>, you
will almost certain describe as "an instance variable of the singleton
class of the class A", or something like that. There's almost
certainly no need to create a separate term for it.
Mind you, I don't think I've ever seen such an instance variable, so
it's probably not a big problem 
I agree that since a singleton class is some sort of class, it can be
called a "class instance variable". But my question are "how do I access
it ?" and "Is it any usefull" (I understand why first and second can be
used, but not third).
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.
I don't want to put @my_variable here, I want to use the wrapper method
you're talking about, but I don't know how to build it 
You can always write a wrapper like this:
def self.x
@x
end
or you can write an x method in the class or singleton class of x.
Similarly for x= (the setter method).
You can also use the attr_* family of methods to create the wrapper
method(s) for you.
To do that in your example, you'd do:
class A
class << self
class << self
attr_accessor :x
end
end
end
But you'll never see this much nesting!
A more common case might
be:
class A
end
a = A.new
class << a
attr_accessor :x
end
which creates attribute get/set methods for 'x' on the object a.
David
···
On Fri, 17 Aug 2007, Arno J. wrote:
--
* 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)