class-level methods and variables

R

rocky.stevens

I am just starting Ruby, and am a bit confused about how class-level
methods and variables are implemented. I had expected them to be
implemented the same way as an instance method/variable on the the
specific class object itself. For example, I thought static1 and
static2 would be implemented the same way below:

-------------------------------------------------
class C1
@@static1 = "@@static1"

def C1.static1
@@static1
end
end

C1.instance_eval do
@static2 = "@static2"

def static2
@static2
end
end
---------------------------------------------

Now, when I call C1.static1 and C2.static2, I get "@@static1" and
"@static2", respectively. So far, so good. But if I make an empty
class C2 which derives off of C1, then C2.static1 and C2.static2
resolve to "@@static1" and nil, respectively.

So it seems to me that when a class tries to access a "@@" variable,
Ruby keeps going up the self.superclass chain until it finds it. But
if the class object itself has a "@" variable, this is not done.

All this would be easy to accept, except for the fact that this does
not seem to apply to methods: C2.static2 was a perfectly valid method,
even though I only defined it for C1.

So I guess my question is, have I correctly interpreted the behavior I
am seeing? Am I missing something?
 
J

John Wilger

I am just starting Ruby, and am a bit confused about how class-level
methods and variables are implemented. I had expected them to be
implemented the same way as an instance method/variable on the the
specific class object itself. For example, I thought static1 and
static2 would be implemented the same way below:

-------------------------------------------------
class C1
@@static1 = "@@static1"

def C1.static1
@@static1
end
end

C1.instance_eval do
@static2 = "@static2"

def static2
@static2
end
end
---------------------------------------------

Now, when I call C1.static1 and C2.static2, I get "@@static1" and
"@static2", respectively. So far, so good. But if I make an empty
class C2 which derives off of C1, then C2.static1 and C2.static2
resolve to "@@static1" and nil, respectively.

So it seems to me that when a class tries to access a "@@" variable,
Ruby keeps going up the self.superclass chain until it finds it. But
if the class object itself has a "@" variable, this is not done.

All this would be easy to accept, except for the fact that this does
not seem to apply to methods: C2.static2 was a perfectly valid method,
even though I only defined it for C1.

So I guess my question is, have I correctly interpreted the behavior I
am seeing? Am I missing something?


You're correctly interpreting what actually happens, but sound like
you're unclear on the concepts involved.

C1.static1 uses a class variable. The value of this variable is shared
by the class itself, all instances of the class, all subclasses, and
all instances of subclasses.

C1.static2 uses an instance variable on the object of type Class that
is referenced by the constant C1. Although C2 extends C1, it is a
different object, and therefore has different instance variables.
 
R

rocky.stevens

You're correctly interpreting what actually happens, but sound like
you're unclear on the concepts involved.

C1.static1 uses a class variable. The value of this variable is shared
by the class itself, all instances of the class, all subclasses, and
all instances of subclasses.

C1.static2 uses an instance variable on the object of type Class that
is referenced by the constant C1. Although C2 extends C1, it is a
different object, and therefore has different instance variables.

Thanks for the info. I guess what is still confusing me is, why does
this not work the same way for methods? I would expect the call to
C2.static2 to throw a "method not found" exception, since static2 is
an instance method on the object of type Class that is referenced by
the constant C1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top