[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

class-level methods and variables

rocky.stevens

7/5/2007 5:50:00 PM

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?

2 Answers

John Wilger

7/5/2007 6:03:00 PM

0

On Jul 5, 10:49 am, rocky.stev...@gmail.com wrote:
> 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.

--
Regards,

John Wilger
http://john...

rocky.stevens

7/5/2007 6:09:00 PM

0

On Jul 5, 2:03 pm, John Wilger <johnwil...@gmail.com> wrote:
> 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.
>
> --
> Regards,
>
> John Wilgerhttp://john...

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.