Class Methods and derivation

Discussion in 'Ruby' started by Damphyr, Sep 28, 2005.

  1. Damphyr

    Damphyr Guest

    I do something like the following

    class Test
    def Test.open
    tt=Test.new
    yield tt
    tt.close
    end
    end

    class DerivedTest<Test
    include SomeModule
    end

    Obviously the following will fail

    DerivedTest.open{|tt|
    tt.some_module_method
    }

    so I moved open to DerivedTest.

    Can I write a class method that instantiates the proper derived class?
    I need a way to find the class of the caller and instantiate that :)
    Am I asking for the impossible? I think not, since Io_Open does this,
    although the code is C - the call to rb_class_new_instance obviously
    instantiates the proper class. How can I do that in pure Ruby?
    Cheers,
    V.-
    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
    Damphyr, Sep 28, 2005
    #1
    1. Advertising

  2. Damphyr

    ts Guest

    >>>>> "D" == Damphyr <> writes:

    D> I do something like the following

    Try something like this

    moulon% cat b.rb
    #!/usr/bin/ruby
    class Test
    def self.open
    tt = new
    yield tt
    ensure
    tt.close if tt
    end

    def close
    end
    end

    module SomeModule
    def some_module_method
    puts "some_module_method"
    end
    end

    class DerivedTest<Test
    include SomeModule
    end

    DerivedTest.open{|tt|
    tt.some_module_method
    }
    moulon%

    moulon% ./b.rb
    some_module_method
    moulon%



    Guy Decoux
    ts, Sep 28, 2005
    #2
    1. Advertising

  3. ts wrote:
    >>>>>> "D" == Damphyr <> writes:

    >
    >> I do something like the following

    >
    > Try something like this
    >
    > moulon% cat b.rb
    > #!/usr/bin/ruby
    > class Test
    > def self.open
    > tt = new
    > yield tt
    > ensure
    > tt.close if tt
    > end
    >
    > def close
    > end
    > end
    >
    > module SomeModule
    > def some_module_method
    > puts "some_module_method"
    > end
    > end
    >
    > class DerivedTest<Test
    > include SomeModule
    > end
    >
    > DerivedTest.open{|tt|
    > tt.some_module_method
    > }
    > moulon%
    >
    > moulon% ./b.rb
    > some_module_method
    > moulon%
    >
    >
    >
    > Guy Decoux


    Guy, you were too fast again! :) So I cut out my similar example and
    just add what would have been my explanation:

    The trick is not to do Test.new but to use just new (calls self.new which
    is DerivedTest in sub class).

    Kind regards

    robert
    Robert Klemme, Sep 28, 2005
    #3
  4. Damphyr

    Damphyr Guest

    Robert Klemme wrote:
    > ts wrote:
    >
    >>>>>>>"D" == Damphyr <> writes:

    >>
    >>>I do something like the following

    >>
    >> Try something like this
    >>
    >>moulon% cat b.rb
    >>#!/usr/bin/ruby
    >>class Test
    >> def self.open
    >> tt = new
    >> yield tt
    >> ensure
    >> tt.close if tt
    >> end
    >>
    >> def close
    >> end
    >>end
    >>
    >>module SomeModule
    >> def some_module_method
    >> puts "some_module_method"
    >> end
    >>end
    >>
    >>class DerivedTest<Test
    >> include SomeModule
    >>end
    >>
    >>DerivedTest.open{|tt|
    >> tt.some_module_method
    >>}
    >>moulon%
    >>
    >>moulon% ./b.rb
    >>some_module_method
    >>moulon%
    >>
    >>
    >>
    >>Guy Decoux

    >
    >
    > Guy, you were too fast again! :) So I cut out my similar example and
    > just add what would have been my explanation:
    >
    > The trick is not to do Test.new but to use just new (calls self.new which
    > is DerivedTest in sub class).
    >
    > Kind regards
    >
    > robert

    I can always rely on Guy for a fast answer :).
    Simple, when not very obvious. I guess I had to keep in mind that new is
    private and for private methods self. is implicit.
    Thanks guys,
    V.-
    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
    Damphyr, Sep 28, 2005
    #4
  5. Damphyr wrote:
    >> Guy, you were too fast again! :) So I cut out my similar example
    >> and just add what would have been my explanation:
    >>
    >> The trick is not to do Test.new but to use just new (calls self.new
    >> which is DerivedTest in sub class).
    >>
    >> Kind regards
    >>
    >> robert

    > I can always rely on Guy for a fast answer :).


    :))

    > Simple, when not very obvious. I guess I had to keep in mind that new
    > is private and for private methods self. is implicit.


    new is *not* private - initialize is. How else could you do Foo.new? new
    is a method of the class intance and initialize is an instance method.

    Kind regards

    robert
    Robert Klemme, Sep 29, 2005
    #5
  6. Damphyr

    Damphyr Guest

    Robert Klemme wrote:
    > Damphyr wrote:
    >
    >>>Guy, you were too fast again! :) So I cut out my similar example
    >>>and just add what would have been my explanation:
    >>>
    >>>The trick is not to do Test.new but to use just new (calls self.new
    >>>which is DerivedTest in sub class).
    >>>
    >>>Kind regards
    >>>
    >>> robert

    >>
    >>I can always rely on Guy for a fast answer :).

    >
    >
    > :))
    >
    >
    >>Simple, when not very obvious. I guess I had to keep in mind that new
    >>is private and for private methods self. is implicit.

    >
    >
    > new is *not* private - initialize is. How else could you do Foo.new? new
    > is a method of the class intance and initialize is an instance method.

    Forget even that I said it. It was too late after too many beers :)
    V.-

    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
    Damphyr, Sep 29, 2005
    #6
  7. Damphyr wrote:
    > Robert Klemme wrote:
    >> Damphyr wrote:
    >>
    >>>> Guy, you were too fast again! :) So I cut out my similar example
    >>>> and just add what would have been my explanation:
    >>>>
    >>>> The trick is not to do Test.new but to use just new (calls self.new
    >>>> which is DerivedTest in sub class).
    >>>>
    >>>> Kind regards
    >>>>
    >>>> robert
    >>>
    >>> I can always rely on Guy for a fast answer :).

    >>
    >>
    >> :))
    >>
    >>
    >>> Simple, when not very obvious. I guess I had to keep in mind that
    >>> new is private and for private methods self. is implicit.

    >>
    >>
    >> new is *not* private - initialize is. How else could you do
    >> Foo.new? new is a method of the class intance and initialize is an
    >> instance method.

    > Forget even that I said it. It was too late after too many beers :)


    <sonorous voice>don't drink and code</sonorous voice>

    :)

    robert
    Robert Klemme, Sep 29, 2005
    #7
  8. Damphyr

    Damphyr Guest

    Robert Klemme wrote:
    > Damphyr wrote:
    >=20
    >> Robert Klemme wrote:
    >>=20
    >>> Damphyr wrote:
    >>>=20

    snip
    >>>> Simple, when not very obvious. I guess I had to keep in mind=20
    >>>> that new is private and for private methods self. is implicit.
    >>>=20
    >>>=20
    >>> new is *not* private - initialize is. How else could you do=20
    >>> Foo.new? new is a method of the class intance and initialize is=20
    >>> an instance method.

    >>=20
    >> Forget even that I said it. It was too late after too many beers :)=20

    > <sonorous voice>don't drink and code</sonorous voice>

    The thing is, I woke up today and the first thing I thought was: "&=A7$&!=
    =20
    that phrase about new is soooo wrong. What was I thinking?"
    So the moral of the story is:
    Don't drink and post.

    If you code then you can always run your (ever present) unit tests and=20
    catch it later (there's always a second chance). No way to avoid public=20
    humiliation if you post though :)
    V.-
    --=20
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - =E4=F9=F1=E5=DC=ED =F5=F0=E7=F1=E5=F3=DF=E1 =E7=EB=
    =E5=EA=F4=F1=EF=ED=E9=EA=EF=FD =F4=E1=F7=F5=E4=F1=EF=EC=E5=DF=EF=F5.
    http://www.freemail.gr - free email service for the Greek-speaking.
    Damphyr, Sep 29, 2005
    #8
  9. Robert Klemme wrote:

    ><sonorous voice>don't drink and code</sonorous voice>
    >
    >

    Seriously, drinking and coding is a great combination. It made a lot of
    my programming assignments in college worth doing.

    Devin
    Devin Mullins, Sep 29, 2005
    #9
  10. On Sep 29, 2005, at 6:24 AM, Devin Mullins wrote:
    > Robert Klemme wrote:
    >> <sonorous voice>don't drink and code</sonorous voice>

    > Seriously, drinking and coding is a great combination. It made a
    > lot of my programming assignments in college worth doing.


    Yeah, but then you end up with a lot of variable names like 'sausage'
    and 'bratwurst' and 'bun', which all seemed hilarious and vaguely
    appropriate at the time, but which make no sense one week later.

    Not that I have any experience with that.
    Gavin Kistner, Sep 29, 2005
    #10
  11. Gavin Kistner <> wrote:
    > On Sep 29, 2005, at 6:24 AM, Devin Mullins wrote:
    > > Robert Klemme wrote:
    > >> <sonorous voice>don't drink and code</sonorous voice>

    > > Seriously, drinking and coding is a great combination. It made a
    > > lot of my programming assignments in college worth doing.

    >
    > Yeah, but then you end up with a lot of variable names like 'sausage'
    > and 'bratwurst' and 'bun', which all seemed hilarious and vaguely
    > appropriate at the time, but which make no sense one week later.


    Obviously they were approximations to chunky_bacon :)

    martin
    Martin DeMello, Sep 30, 2005
    #11
    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. Steven T. Hatton
    Replies:
    12
    Views:
    1,694
    Jonathan Turkanis
    Aug 20, 2004
  2. maadhuu
    Replies:
    3
    Views:
    4,077
    Alf P. Steinbach
    May 17, 2005
  3. Pierre Rouleau
    Replies:
    2
    Views:
    386
    Pierre Rouleau
    Apr 25, 2005
  4. Massimo Soricetti
    Replies:
    6
    Views:
    469
    Daniel T.
    Feb 2, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    310
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page