Class variables in an inheritance hierarchy

Discussion in 'Ruby' started by Todd Breiholz, Jan 20, 2006.

  1. ------=_Part_13289_32734560.1137773209543
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    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 o=
    f
    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

    ------=_Part_13289_32734560.1137773209543--
     
    Todd Breiholz, Jan 20, 2006
    #1
    1. Advertising

  2. Todd Breiholz

    Guest

    Hi --

    On Sat, 21 Jan 2006, 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.


    Account.fields and Opportunity fields are methods, not variables. You
    can easily define them:

    class Account < Base
    def self.fields
    # code here, possibly using instance variable to hold info
    end
    # etc.
    end

    Or you could put it in a module and extend the various classes with
    it. (The best exact way will depend on what's in the method.)

    For the instances:

    class Base
    def fields
    self.class.fields
    end
    end

    That way, each object will know to query its own class to get the
    right fields method.


    David

    --
    David A. Black


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black
     
    , Jan 20, 2006
    #2
    1. Advertising

  3. 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-forum.com/.
     
    Eero Saynatkari, Jan 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    452
    H.MuthuKumaraRajan
    Feb 4, 2004
  2. Rob Kirkpatrick

    Visualize class inheritance hierarchy

    Rob Kirkpatrick, Sep 23, 2008, in forum: Python
    Replies:
    3
    Views:
    342
    Rob Kirkpatrick
    Sep 24, 2008
  3. kmw
    Replies:
    5
    Views:
    1,864
    James Kanze
    Oct 29, 2009
  4. Mehr, Assaph (Assaph)
    Replies:
    1
    Views:
    499
    Simon Strandgaard
    Apr 15, 2004
  5. Minkoo Seo
    Replies:
    0
    Views:
    92
    Minkoo Seo
    Feb 28, 2006
Loading...

Share This Page