where's the best place to dynamically add singleton method?

Discussion in 'Ruby' started by Iain Barnett, Apr 28, 2011.

  1. Iain Barnett

    Iain Barnett Guest

    Hi,

    If I've got a simple bit of initialisation code

    def initialize( blah )
    @listener =3D Blah.new blah
    end

    and I'd like to add a singleton method to instance (because I don't want =
    to monkey patch the Blah library) where's the best place to do that? I =
    know I could do:

    def initialize( blah )
    @listener =3D Blah.new blah
    def @listener.my_mental_method
    self.give_it_up! "quickly"
    end
    end

    but is this a good place to do it, or is there something more =
    eloquent/perfomant? I'd like all instances, wherever they are created, =
    to have this method, so is there a better way altogether?

    Just wondering.

    Regards,
    Iain=
     
    Iain Barnett, Apr 28, 2011
    #1
    1. Advertising

  2. On Thu, Apr 28, 2011 at 3:58 PM, Iain Barnett <> wrote:
    > Hi,
    >
    > If I've got a simple bit of initialisation code
    >
    > =A0 =A0def initialize( blah )
    > =A0 =A0 =A0@listener =3D Blah.new blah
    > =A0 =A0end
    >
    > and I'd like to add a singleton method to instance (because I don't want =

    to monkey patch the Blah library) where's the best place to do that? I know=
    I could do:
    >
    > =A0 =A0def initialize( blah )
    > =A0 =A0 =A0@listener =3D Blah.new blah
    > =A0 =A0 =A0def @listener.my_mental_method
    > =A0 =A0 =A0 =A0self.give_it_up! "quickly"


    No self needed here.

    > =A0 =A0 =A0end
    > =A0 =A0end
    >
    > but is this a good place to do it, or is there something more eloquent/pe=

    rfomant? I'd like all instances, wherever they are created, to have this me=
    thod, so is there a better way altogether?

    First of all, I'd place the method in a module so you have less
    definitions around. Then I'd extend all instances with that module.
    That also makes for easy addition of more methods.

    module BlahExt
    def your_mental_method
    give_it_up! 'rather sooner than later'
    end
    end

    The automatic part could be handled by changing Bar's #new:

    class <<Blah
    alias _new new

    def new(*a,&b)
    _new(*a,&b).extend BlahExt
    end
    end

    Note that this does not deal with inheritance nicely. For that you
    could do something similar with Bar's #initialize instead of #new.

    If you do not need a global automatic I'd simply stick with

    def initialize( blah )
    @listener =3D Blah.new(blah).extend BlahExt
    end

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 28, 2011
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    I think you can achieve functionality that you desire by using mixins,
    modules and include statement... like this


    module CoolMethods
    def my_mental_method
    ....
    end
    end

    class UnfortunateUserOfCoolMethod

    include CoolMethods
    ....

    end

    Also if you need method on a class you can use extend intstead of
    include....


    Best Regards,

    Kresimir Bojcic

    www.kresimirbojcic.com


    On Thu, Apr 28, 2011 at 3:58 PM, Iain Barnett <> wrote:

    > Hi,
    >
    > If I've got a simple bit of initialisation code
    >
    > def initialize( blah )
    > @listener = Blah.new blah
    > end
    >
    > and I'd like to add a singleton method to instance (because I don't want to
    > monkey patch the Blah library) where's the best place to do that? I know I
    > could do:
    >
    > def initialize( blah )
    > @listener = Blah.new blah
    > def @listener.my_mental_method
    > self.give_it_up! "quickly"
    > end
    > end
    >
    > but is this a good place to do it, or is there something more
    > eloquent/perfomant? I'd like all instances, wherever they are created, to
    > have this method, so is there a better way altogether?
    >
    > Just wondering.
    >
    > Regards,
    > Iain
    >
     
    Kresimir Bojcic, Apr 28, 2011
    #3
  4. Iain Barnett

    Iain Barnett Guest

    On 28 Apr 2011, at 15:09, Robert Klemme wrote:

    > On Thu, Apr 28, 2011 at 3:58 PM, Iain Barnett <> =

    wrote:
    >> Hi,
    >>=20
    >> If I've got a simple bit of initialisation code
    >>=20
    >> def initialize( blah )
    >> @listener =3D Blah.new blah
    >> end
    >>=20
    >> and I'd like to add a singleton method to instance (because I don't =

    want to monkey patch the Blah library) where's the best place to do =
    that? I know I could do:
    >>=20
    >> def initialize( blah )
    >> @listener =3D Blah.new blah
    >> def @listener.my_mental_method
    >> self.give_it_up! "quickly"

    >=20
    > No self needed here.
    >=20
    >> end
    >> end
    >>=20
    >> but is this a good place to do it, or is there something more =

    eloquent/perfomant? I'd like all instances, wherever they are created, =
    to have this method, so is there a better way altogether?
    >=20
    > First of all, I'd place the method in a module so you have less
    > definitions around. Then I'd extend all instances with that module.
    > That also makes for easy addition of more methods.
    >=20
    > module BlahExt
    > def your_mental_method
    > give_it_up! 'rather sooner than later'
    > end
    > end
    >=20
    > The automatic part could be handled by changing Bar's #new:
    >=20
    > class <<Blah
    > alias _new new
    >=20
    > def new(*a,&b)
    > _new(*a,&b).extend BlahExt
    > end
    > end
    >=20
    > Note that this does not deal with inheritance nicely. For that you
    > could do something similar with Bar's #initialize instead of #new.
    >=20
    > If you do not need a global automatic I'd simply stick with
    >=20
    > def initialize( blah )
    > @listener =3D Blah.new(blah).extend BlahExt
    > end
    >=20
    > Kind regards
    >=20
    > robert
    >=20
    > --=20
    > remember.guy do |as, often| as.you_can - without end
    > http://blog.rubybestpractices.com/
    >=20



    On 28 Apr 2011, at 15:15, Kresimir Bojcic wrote:

    > I think you can achieve functionality that you desire by using mixins,
    > modules and include statement... like this
    >=20
    >=20
    > module CoolMethods
    > def my_mental_method
    > ....
    > end
    > end
    >=20
    > class UnfortunateUserOfCoolMethod
    >=20
    > include CoolMethods
    > ....
    >=20
    > end
    >=20
    > Also if you need method on a class you can use extend intstead of
    > include....
    >=20
    >=20
    > Best Regards,
    >=20
    > Kresimir Bojcic
    >=20
    > www.kresimirbojcic.com



    Thanks to you both for the input. Sorry I didn't reply sooner but was =
    snowed under with other things and wanted to think about it and do a bit =
    of research. I've been reading a lot more on what's going on with =
    details of the Ruby inheritance model, all that stuff in the background =
    that I was getting along with fine without till now (which I think is a =
    testament to the language), and your answers helped.=20

    Due to this I refactored things quite significantly so I've ended up not =
    using this directly, but I've gone further for it.

    Much appreciated

    Regards,
    Iain
     
    Iain Barnett, May 6, 2011
    #4
  5. On 06.05.2011 22:15, Iain Barnett wrote:

    > Thanks to you both for the input. Sorry I didn't reply sooner but was
    > snowed under with other things and wanted to think about it and do a
    > bit of research. I've been reading a lot more on what's going on with
    > details of the Ruby inheritance model, all that stuff in the
    > background that I was getting along with fine without till now (which
    > I think is a testament to the language), and your answers helped.
    >
    > Due to this I refactored things quite significantly so I've ended up
    > not using this directly, but I've gone further for it.
    >
    > Much appreciated


    I'm glad to hear that we could help you. Thank you for letting us know!

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 7, 2011
    #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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,261
    Proton Projects - Moin
    Mar 27, 2007
  2. rs387
    Replies:
    3
    Views:
    272
    rs387
    Oct 2, 2008
  3. Wilhelm
    Replies:
    1
    Views:
    167
  4. Trans
    Replies:
    12
    Views:
    281
    Robert Klemme
    Sep 14, 2007
  5. Replies:
    15
    Views:
    773
Loading...

Share This Page