[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Class variables in an inheritance hierarchy

Todd Breiholz

1/20/2006 4:07:00 PM

Assume the following structure:

class Base
end

class Account < Base
end

class Opportunity < Base
end


I want to have a class variable Account.fields that is different than
Opportunity.fields, so that all instances of Account can reference the
Account.fields and all instances if Opportunity can reference
Opportunity.fields and get the correct results.

As a reference, I am trying to create something akin to ActiveRecord (for a
different type of datasource) where each distinct object has it's own set of
attributes. I've been looking at the ActiveRecord code, but being new to
Ruby it's giving me a bit of a headache :)

Thanks!

Todd Breiholz
2 Answers

dblack

1/20/2006 6:39:00 PM

0

Eero Saynatkari

1/20/2006 6:58:00 PM

0

Todd Breiholz wrote:
> Assume the following structure:
>
> class Base
> end
>
> class Account < Base
> end
>
> class Opportunity < Base
> end
>
>
> I want to have a class variable Account.fields that is different than
> Opportunity.fields, so that all instances of Account can reference the
> Account.fields and all instances if Opportunity can reference
> Opportunity.fields and get the correct results.
>
> As a reference, I am trying to create something akin to ActiveRecord
> (for a
> different type of datasource) where each distinct object has it's own
> set of
> attributes. I've been looking at the ActiveRecord code, but being new to
> Ruby it's giving me a bit of a headache :)

You can use class instance variables (Classes Are Objects Too(tm) :)

class Account < Base
# We are in the context of the class so this variable
# will belong to the class, not its instances.
@fields = something
end

You would also need to set up accessors for the variables.

> Thanks!
>
> Todd Breiholz


E


--
Posted via http://www.ruby-....