[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Attempted roadmap of future instance variables....

Christoph R.

12/2/2003 4:07:00 PM

David A. Black wrote:
...
> Hi --
>
> I figure that I owe the world at least a shot at clearing the
> cobwebs (of my own creation, that is :-) on this instance
> variable thing.
>
> So... for those still reading... here's what I *really* think is being
> proposed:

I do ...

>
>
> class A
> def m
> @var = "hi" # regular instance variable
> @_var = "hello" # class-local instance variable
> end
> end
>
> class B < A
> def n
> puts @var # same as @var, above
> puts @_var # not same as @_var above
> end
> end
>
> o = B.new
> o.m
> o.n
>
> => Output:
>
> hi # the @var that was set in A#m is used here
> nil # the @_var that was in A#m is *not* used here
> # (because we're now in a method defined in B,
> # a subclass of A, and class local instance
> # variables are not shared by subclasses)
>
> In other words:
>
> * Regular instance variables live per-name per-object
> (o has exactly one @var).
>
> * Class-local instance variables live per-name per-object per-class
> (o's methods defined in A have a @_var, and o's methods defined in
> B have a different @_var).

Thank you very for this comprehensive write up ...


/Christoph