Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3622 > unrolled thread

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

Started byIain Barnett <iainspeed@gmail.com>
First post2011-04-28 08:58 -0500
Last post2011-04-28 09:15 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  where's the best place to dynamically add singleton method? Iain Barnett <iainspeed@gmail.com> - 2011-04-28 08:58 -0500
    Re: where's the best place to dynamically add singleton method? Robert Klemme <shortcutter@googlemail.com> - 2011-04-28 09:09 -0500
      Re: where's the best place to dynamically add singleton method? Iain Barnett <iainspeed@gmail.com> - 2011-05-06 15:15 -0500
        Re: where's the best place to dynamically add singleton method? Robert Klemme <shortcutter@googlemail.com> - 2011-05-07 14:29 +0200
    Re: where's the best place to dynamically add singleton method? Kresimir Bojcic <kresimir.bojcic@gmail.com> - 2011-04-28 09:15 -0500

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

FromIain Barnett <iainspeed@gmail.com>
Date2011-04-28 08:58 -0500
Subjectwhere's the best place to dynamically add singleton method?
Message-ID<44FF3A41-4EBD-4404-A8A7-5216D77291DA@gmail.com>
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

[toc] | [next] | [standalone]


#3625

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-28 09:09 -0500
Message-ID<BANLkTimvU4EPZiQ6n9wSom5C3ydQeXvGFQ@mail.gmail.com>
In reply to#3622
On Thu, Apr 28, 2011 at 3:58 PM, Iain Barnett <iainspeed@gmail.com> 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"

No self needed here.

>      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?

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 = Blah.new(blah).extend BlahExt
   end

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#4053

FromIain Barnett <iainspeed@gmail.com>
Date2011-05-06 15:15 -0500
Message-ID<024A73D9-D6FE-4F3F-B518-F39206A6F37B@gmail.com>
In reply to#3625
On 28 Apr 2011, at 15:09, Robert Klemme wrote:

> On Thu, Apr 28, 2011 at 3:58 PM, Iain Barnett <iainspeed@gmail.com> 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"
> 
> No self needed here.
> 
>>      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?
> 
> 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 = Blah.new(blah).extend BlahExt
>   end
> 
> Kind regards
> 
> robert
> 
> -- 
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
> 


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
> 
> 
> 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


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

Regards,
Iain

[toc] | [prev] | [next] | [standalone]


#4063

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-05-07 14:29 +0200
Message-ID<92ks99F131U1@mid.individual.net>
In reply to#4053
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/

[toc] | [prev] | [next] | [standalone]


#3626

FromKresimir Bojcic <kresimir.bojcic@gmail.com>
Date2011-04-28 09:15 -0500
Message-ID<BANLkTi=Ka-=q8gCJVrgHNUB3mC2rWhV7KA@mail.gmail.com>
In reply to#3622
[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 <iainspeed@gmail.com> 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
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web