[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: simple subclass question

Ball, Donald A Jr (Library)

5/1/2007 9:45:00 PM

Actually, I spoke too quickly. If I use this construct:

> class A
> class << self
> attr_accessor :var
> end
> end
>
> class B < A
> class << self
> def var
> @var || superclass.var
> end
> end
> end

How can I initialize var in A? I've tried:

class A
class << self
@var = 'foo'
attr_accessor :var
end
end

to no avail. Also tried doing it in an initialize method. I'm still
missing something key, I think.

- donald

3 Answers

Rick DeNatale

5/2/2007 3:33:00 PM

0

On 5/1/07, Logan Capaldo <logancapaldo@gmail.com> wrote:
> On 5/1/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov> wrote:
> >
> >
> > class A
> > class << self
> > @var = 'foo'
> > attr_accessor :var
> > end
> > end
> >
> > to no avail. Also tried doing it in an initialize method. I'm still
> > missing something key, I think.
>
>
> class A
> @var = 'foo'
> class << self
> attr_accessor :var
> end
> end
>
> (warning, the following explanation is not really correct)
> class << self is sort of A's class (the class of the class) (It's actually
> A's singleton class). By putting @var='foo' inside class << self you set the
> class of A's instance var @var to 'foo' not A's @var to foo.

Going a little further in answering Donald's question.

Instance variables come into being when they are initialized by code
running in the context of the instance. Usually that's method code.

This works in the case of class methods, class instance variables are
just instance variables of the class.

However since, I think what we are looking for is a way to initialize
class instance variables in a way analogous to class variables, we
really don't always want to define a method. Here's one way to
accomplish class instance variable intialization in-line in a class
(re)definition.

class A
instance_eval {@var = 'foo'}
class << self
attr_accessor :var
end
end

since within the class (re)definition self IS the class, instance_eval
runs the block in the context of the class.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Robert Dober

5/2/2007 3:47:00 PM

0

On 5/2/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
<snip>
>
> class A
> instance_eval {@var = 'foo'}
I guess I lost you here Rick, is this for didactic purpose, or am I
wrong by saying that

instance_eval { @var = 'foo' }

is *exactly* the same as

@var = 'foo'

....
<snip>

Cheers
Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Rick DeNatale

5/3/2007 12:04:00 PM

0

On 5/2/07, Robert Dober <robert.dober@gmail.com> wrote:
> On 5/2/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> <snip>
> >
> > class A
> > instance_eval {@var = 'foo'}
> I guess I lost you here Rick, is this for didactic purpose, or am I
> wrong by saying that
>
> instance_eval { @var = 'foo' }
>
> is *exactly* the same as
>
> @var = 'foo'
>
> ....
> <snip>

D'oh! You're right of course.

It's like those times when I ask my wife if she's seen my eyeglasses
and she tells me that I'm wearing them!

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...