[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 5:29:00 PM

Maybe I am being dense, so bear with me...

So @_var does not refer to the class instance variable @var? If @_var
defined in a class context is different from @_var in an instance then how
does one refer to the @_var created in the instance context in the class
context?

I would think that @_var in the instance context should be the same as
refering to the @var in the class context. It would be like having
attr_accessor for @var with a little sugar for the instance context.

Steve Tuckner

> -----Original Message-----
> From: David A. Black [mailto:dblack@wobblini.net]
> Sent: Tuesday, December 02, 2003 10:56 AM
> To: ruby-talk ML
> Subject: Re: Attempted roadmap of future instance variables....
>
>
> Hi --
>
> On Wed, 3 Dec 2003, Steve Tuckner wrote:
>
> > 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?
>
> I think that @_ and @ variables would behave identically when it came
> to Class objects' instance variables -- simply because you can't
> subclass Class, so the issue of whether the same or an old or new
> variable is in use would never arise. In other words, the situation
> exactly analogous to what I did before would be:
>
> class Class
> def m
> @var = "hi"
> @_var = "hello"
> end
> end
>
> class B < Class # error
> def n
> puts @var
> puts @_var
> end
> end
>
> which can never happen.
>
> Also, I assume (and hope :-) that all of this will still be governed
> by the "every object can have its own instance variables" principle.
> That means that if an object is a Class, and has its own instance
> variables, they will not be directly visible to other objects
> (including instances of that class).
>
> Therefore, in your example:
>
> > 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}"
>
> @_a here would refer to the @_a of an instance of A, not A's own @_a.
> And an instance of A, say 'a' in:
>
> a = A.new
>
> is a different object -- a different 'self' -- from A, and therefore
> does not have direct lexical access to A's own instance variables,
> only its own.
>
> That, in turn, means you've asked it to display an uninitialized
> instance variable, so here:
>
> > A.new.display
> > => a (class local - current) = 1
> > => a (class local - new) = 2
>
> you should get nil, not 2.
>
>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>

2 Answers

ts

12/2/2003 5:36:00 PM

0

>>>>> "S" == Steve Tuckner <STUCKNER@MULTITECH.COM> writes:

S> So @_var does not refer to the class instance variable @var? If @_var
S> defined in a class context is different from @_var in an instance then how
S> does one refer to the @_var created in the instance context in the class
S> context?

@_var defined in a class context is different from @_var in an instance

like

@var defined in a class context is different from @var in an instance


See gsub!(/@_/, '@') # :-)


Guy Decoux



Lyle Johnson

12/2/2003 5:54:00 PM

0

Steve Tuckner wrote:

> So @_var does not refer to the class instance variable @var?

Correct. @_var and @var refer to two *different* instance variables.

> If @_var defined in a class context is different from @_var in an instance then how
> does one refer to the @_var created in the instance context in the class
> context?

If I parsed that question correctly, the answer is "you don't" ;)

Generally speaking, there isn't any way for the *class* of an instance
to refer to its instance's instance variables.

> I would think that @_var in the instance context should be the same as
> refering to the @var in the class context. It would be like having
> attr_accessor for @var with a little sugar for the instance context.

I'm not sure, but I think you may be under the impression that @_var and
@var are somehow linked to each other, and they aren't. (Perhaps David
should have used completely different names in the original example).

For C++ programmers, the easiest way to think of this is that a Ruby
instance variable whose name starts with an underscore (such as @_foo)
is private, in the C++ sense. An instance variable whose name doesn't
start with an underscore (such as @bar) is protected, again in the C++
sense.

Unless, of course, I am completely confused ;)