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

Thanks to you and Robert for the quick answers. I think I get it now; I
was a little fuzzy on the notion of class instance variables before. I
now use a construct like so:

class A
class << self
attr_accessor :var
end
end

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

to allow subclasses's instance variables to default to their
superclass's until and unless overridden. I'll admit the class << self
syntax continues to mystify somewhat; can anyone proffer an explanation
that would help me grok it fully?

- donald

4 Answers

Robert Dober

5/1/2007 10:11:00 PM

0

On 5/1/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov> wrote:
> Thanks to you and Robert for the quick answers. I think I get it now; I
> was a little fuzzy on the notion of class instance variables before. I
> now use a construct like so:
>
> class A
> class << self
> attr_accessor :var
> end
> end
>
> class B < A
> class << self
> def var
> @var || superclass.var
> end
> end
> end
>
> to allow subclasses's instance variables to default to their
> superclass's until and unless overridden.

That was the point I missed, but of course you did that nicely :).
I'll admit the class << self
> syntax continues to mystify somewhat; can anyone proffer an explanation
> that would help me grok it fully?
I will take a chance by saying that

class << A
def x...
end
end

is the same as

class A
def self.x
end
end

although there might be some subtle differences

Do e.g, this
class << A
puts self
end
you can see kind of a Proxy object, but it behaves pretty much transparently.

Cheers
Robert

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

dblack

5/1/2007 11:27:00 PM

0

Hi --

On 5/1/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov> wrote:
> Thanks to you and Robert for the quick answers. I think I get it now; I
> was a little fuzzy on the notion of class instance variables before. I
> now use a construct like so:
>
> class A
> class << self
> attr_accessor :var
> end
> end
>
> class B < A
> class << self
> def var
> @var || superclass.var
> end
> end
> end
>
> to allow subclasses's instance variables to default to their
> superclass's until and unless overridden. I'll admit the class << self
> syntax continues to mystify somewhat; can anyone proffer an explanation
> that would help me grok it fully?

Possibly; have a look at: http://www.rubypal.com/singl...


David

--
Upcoming Rails training by Ruby Power and Light:
Four-day Intro to Intermediate
May 8-11, 2007
Edison, NJ
http://www.rubypal.com/event...

Brian Candler

5/2/2007 5:38:00 AM

0

On Wed, May 02, 2007 at 06:33:57AM +0900, Ball, Donald A Jr (Library) wrote:
> I'll admit the class << self
> syntax continues to mystify somewhat; can anyone proffer an explanation
> that would help me grok it fully?

http://www.rubygarden.org/ruby?Singlet...

Harold Hausman

5/2/2007 6:27:00 AM

0

On 5/2/07, Brian Candler <B.Candler@pobox.com> wrote:
> On Wed, May 02, 2007 at 06:33:57AM +0900, Ball, Donald A Jr (Library) wrote:
> > I'll admit the class << self
> > syntax continues to mystify somewhat; can anyone proffer an explanation
> > that would help me grok it fully?
>
> http://www.rubygarden.org/ruby?Singlet...
>
>

I always liked this explanation:
http://whytheluckystiff.net/articles/seeingMetaclassesCl...

hth,
-Harold