[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....

Steve Tuckner

12/2/2003 4:33:00 PM

So what is the relationship between @_ vars and @vars that are defined in a
class context (if any)? Is this just to allow easier access to class local
vars (that you can already define)? Does below describe the situation
accurately?

class A
@a = 1 # a is a class local variable
@_a = 2 # a is also a class local variable
@@a = 3 # a is a global variable whose visibility is itself and its
sub-classes

def A.a # define to allow access to class's instance var
@a
end

def display
puts "a (class local - current) = #{self.class.a}"
puts "a (class local - new) = #{@_a}"
puts "a (class global) = #{@@a}"
end
end

class B < A
end

A.new.display
=> a (class local - current) = 1
=> a (class local - new) = 2
=> a (class global) = 3

B.new.display
=> a (class local - current) =
=> a (class local - new) =
=> a (class global) = 3

Steve Tuckner

> -----Original Message-----
> From: David A. Black [mailto:dblack@wobblini.net]
> Sent: Tuesday, December 02, 2003 9:24 AM
> To: ruby-talk ML
> Subject: Attempted roadmap of future instance variables....
>
>
> 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:
>
>
> 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).
>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>

1 Answer

eddysterckx@hotmail.com

12/29/2009 8:34:00 AM

0

On 29 dec, 09:05, "M. Ziegler" <ziegle...@gmx.de> wrote:
> > On Mon, 28 Dec 2009 06:49:37 -0800 (PST), "eddyster...@hotmail.com"
> > <eddyster...@hotmail.com> wrote:
>
>   Some belts are overlapping f.e. south Germany was inside both
>
> > beer and wine belts. Scandinavia was beer and vodka belt(s).
>
> South West Germany is more a wine belt, the rest a beer belt.

I've not seen the map Mr. Mastruko refers to but I guess it's more or
less a climate belt map as well. warm and sunny -> wine, mild -> beer,
cold -> vodka

That said, over the past 20 years I've seen France evolve from a
country where the only drinks you could get in a pub were wine and
pastis to a country where even in the most remote parts they've got
good beer on tap.

Greetz,

Eddy Sterckx