I am new to ruby. I have understood the singleton class concept of ruby but I have one question that at what point ruby creates a singleton class for objects. I mean when objects define their own method(which is not in its ancestor class or super class) or when the object is created at runtime. I am clear with the concept that when a Class or object define methods as their own methods an anonymous class inserted between the object and its class which is know as 'Singleton Class'. But looking at this thread Difference between class variables and class instance variables? in banister answer he has used something like
hello.singleton_class.class_variable_get(:@@class_var)
where hello is an object of a class Hello
which has class variable @@class_var
.
Why do we have to use 'singleton_class' in
hello.singleton_class.class_variable_get(:@@class_var)
Does the singleton class also get inserted when you define class variables in a class?