How to call a class method when (i.e. in the moment of) inheritingfrom a class/defining a descendant

Discussion in 'Ruby' started by Thomas, Jun 6, 2005.

  1. Thomas

    Thomas Guest

    Hi,

    This problem maybe results from a bad understanding of what I'm trying
    to do. Anyway, is it possible to call a method when creating a new class
    or inheriting from another class?

    Currently my code looks somehow like this:

    class A
    class << self
    attr_reader :x

    def prepare
    # do something
    end

    def fixate
    # do something
    end

    def foo(a)
    # do something with a and @x
    end
    end
    end

    class B < A
    prepare

    @x = "something"

    foo("bar")

    def method1
    # do something else
    end

    fixate
    end

    Now in an ideal world, the definition of class B would be simply:

    class B < A
    @x = "something"

    def method1
    # do something
    end
    end

    and A.setup and A.fixate should be automagically invoked whenever
    defining a descendant from class A.

    Does somebody know a solution for how this can be done? The only
    possible way of doing this I could come up with is to wrap the
    definition part into a block and pass this block to a method that calls
    these functions and evaluates the block using class_eval. Something in
    the line of (untested):

    class C < A
    wrapper do
    @x = "something"

    def method1
    # do something
    end
    end
    end

    But I'm not sure if this solution really is an advantage.

    Cheers,
    Thomas.
    Thomas, Jun 6, 2005
    #1
    1. Advertising

  2. You must keep in mind that a class instance variable is local for the
    particular class object. In your case, trying to set @x in B does it
    locally for class B, not affecting @x in class A in any way, even when B
    inherits from A. You may want to define "attr_writer :x" in class A and
    use "self.x = 'something'" in class B.

    Gennady.

    Thomas wrote:
    > Hi,
    >
    > This problem maybe results from a bad understanding of what I'm trying
    > to do. Anyway, is it possible to call a method when creating a new class
    > or inheriting from another class?
    >
    > Currently my code looks somehow like this:
    >
    > class A
    > class << self
    > attr_reader :x
    >
    > def prepare
    > # do something
    > end
    >
    > def fixate
    > # do something
    > end
    >
    > def foo(a)
    > # do something with a and @x
    > end
    > end
    > end
    >
    > class B < A
    > prepare
    >
    > @x = "something"
    >
    > foo("bar")
    >
    > def method1
    > # do something else
    > end
    >
    > fixate
    > end
    >
    > Now in an ideal world, the definition of class B would be simply:
    >
    > class B < A
    > @x = "something"
    >
    > def method1
    > # do something
    > end
    > end
    >
    > and A.setup and A.fixate should be automagically invoked whenever
    > defining a descendant from class A.
    >
    > Does somebody know a solution for how this can be done? The only
    > possible way of doing this I could come up with is to wrap the
    > definition part into a block and pass this block to a method that calls
    > these functions and evaluates the block using class_eval. Something in
    > the line of (untested):
    >
    > class C < A
    > wrapper do
    > @x = "something"
    >
    > def method1
    > # do something
    > end
    > end
    > end
    >
    > But I'm not sure if this solution really is an advantage.
    >
    > Cheers,
    > Thomas.
    >
    >
    Gennady Bystritksy, Jun 6, 2005
    #2
    1. Advertising

  3. Thomas ha scritto:

    > Does somebody know a solution for how this can be done?


    see Class#inherited:

    >> class A
    >> def self.inherited(klass)
    >> p "wow I got childs:"+klass.to_s
    >> end
    >> end

    => nil
    >> class A2 < A
    >> end

    "wow I got childs:A2"
    => nil
    >> Class.new(A2)

    "wow I got childs:#<Class:0x2bd54e0>"
    => #<Class:0x2bd54e0>
    gabriele renzi, Jun 6, 2005
    #3
  4. Thomas

    Ara.T.Howard Guest

    On Mon, 6 Jun 2005, Thomas wrote:

    > Hi,
    >
    > This problem maybe results from a bad understanding of what I'm trying to do.
    > Anyway, is it possible to call a method when creating a new class or
    > inheriting from another class?
    >
    > Currently my code looks somehow like this:
    >
    > class A
    > class << self
    > attr_reader :x
    >
    > def prepare
    > # do something
    > end
    >
    > def fixate
    > # do something
    > end
    >
    > def foo(a)
    > # do something with a and @x
    > end
    > end
    > end
    >
    > class B < A
    > prepare
    >
    > @x = "something"
    >
    > foo("bar")
    >
    > def method1
    > # do something else
    > end
    >
    > fixate
    > end
    >
    > Now in an ideal world, the definition of class B would be simply:
    >
    > class B < A
    > @x = "something"
    >
    > def method1
    > # do something
    > end
    > end
    >
    > and A.setup and A.fixate should be automagically invoked whenever defining a
    > descendant from class A.
    >
    > Does somebody know a solution for how this can be done? The only possible way
    > of doing this I could come up with is to wrap the definition part into a
    > block and pass this block to a method that calls these functions and
    > evaluates the block using class_eval. Something in the line of (untested):
    >
    > class C < A
    > wrapper do
    > @x = "something"
    >
    > def method1
    > # do something
    > end
    > end
    > end
    >
    > But I'm not sure if this solution really is an advantage.


    harp:~ > cat a.rb
    class A
    class << self
    def setup
    puts "<#{ self }> invoking setup..."
    end
    def inherited klass
    klass.setup
    end
    end
    end

    class B < A
    end

    harp:~ > ruby a.rb
    <B> invoking setup...

    hth.

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | My religion is very simple. My religion is kindness.
    | --Tenzin Gyatso
    ===============================================================================
    Ara.T.Howard, Jun 6, 2005
    #4
  5. Thomas

    Thomas Guest

    > class A
    > class << self
    > def setup
    > puts "<#{ self }> invoking setup..."
    > end
    > def inherited klass
    > klass.setup
    > end
    > end
    > end
    >
    > class B < A
    > end


    Thanks for this reference to "inherited" -- I didn't know about this
    method and its purpose. As so often with ruby, the solution is somehow
    self-evident/explanatory in retrospect.

    Regards,
    Thomas.
    Thomas, Jun 7, 2005
    #5
    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. Nik Coughin

    css - descendant selectors

    Nik Coughin, Jan 28, 2004, in forum: HTML
    Replies:
    2
    Views:
    379
    Alliss
    Jan 29, 2004
  2. Ales DOLECEK

    Templates and descendant classes

    Ales DOLECEK, Jul 22, 2003, in forum: C++
    Replies:
    2
    Views:
    429
    Rob Williscroft
    Jul 22, 2003
  3. Reid Priedhorsky
    Replies:
    2
    Views:
    326
    Bengt Richter
    Jul 12, 2005
  4. nguillot
    Replies:
    5
    Views:
    520
  5. Chris Morris

    Execute code on descendant class init

    Chris Morris, Jan 3, 2004, in forum: Ruby
    Replies:
    6
    Views:
    107
    Chris Morris
    Jan 7, 2004
Loading...

Share This Page