From: "Damián M. González" Date: 2012-09-16T05:56:44+09:00 Subject: Re: Class variables or class singleton variables? Jan E. wrote in post #1076183: > I don't get what you mean. The singleton class of a class is obviously > not the same as the class itself, so getting different object IDs was to > be expected. Yes. What I did was stupid, but I realized late to edit it. > Maybe you mean something different? If you wonder why I open the > singleton class in the original class with "self" instead of outside the > class with the name: That's both the same, but the first approach is > less redundant and (in my opinion) more readable. But you should > actually use "send". Understood. > And again: You cannot define a class instance variable for Foo in the > singleton class of Foo. Those are two different variables: > > class Foo > @x = 'Foo' > class << self # or class << Foo > @x = 'singleton' > end > end > > [Foo, Foo.singleton_class].each do |c| > puts "#{c}: @x == #{c.instance_variable_get :@x}" > end > > An instance variable always belongs to a specific object instance (hence > the name). So if you define @x within the body of the singleton class, > it will actually belong to the singleton class and not the original > class. Now I know, the class itself is an object which can have instance variables, and the singleton class of a class is kind of another object that can have their own instance variables. class Foo >> singleton class Foo --------- ------------------- @x = 1 @x = 3 #not the same variables Thanks for your help, I appreciate it. -- Posted via http://www.ruby-forum.com/.