[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Class-level readers and writers

dblack

11/29/2003 3:58:00 AM

2 Answers

Hal E. Fulton

11/29/2003 4:45:00 AM

0

David A. Black wrote:
> (Just as a footnote, you can also use "normal" accessor shortcuts at
> the class object level:
>
> class A
> class << self
> attr_accessor :x
> end
> end
>
> A.x = 1 # and so on
>
> which is implemented with instance variables rather than class
> variables, if for any reason that suits your purposes better.)

For the record, this is a cool trick. The reason it isn't in
TRW is simply that I didn't think of it.

The effect is subtly different, but it's a good idiom.

Thanks, David...


Hal




Carl Youngblood

11/29/2003 8:05:00 PM

0

> (Just as a footnote, you can also use "normal" accessor shortcuts at
> the class object level:
>
> class A
> class << self
> attr_accessor :x
> end
> end
>
> A.x = 1 # and so on
>
> which is implemented with instance variables rather than class
> variables, if for any reason that suits your purposes better.)

I'm still trying to figure out what this does, exactly. Is A.x an
instance variable or a class variable? If I have something like:

a = A.new

And I say:

a.x = 1

Is that modifying a class variable or an instance variable? Or does
that even work? I just want to make sure that the accessor will work if
called on either an instance or on the class itself.

Thanks,
Carl