From: Kresimir Bojcic Date: 2011-04-28T23:15:35+09:00 Subject: Re: where's the best place to dynamically add singleton method? --90e6ba53aeccbeef6f04a1fb3008 Content-Type: text/plain; charset=ISO-8859-1 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 > --90e6ba53aeccbeef6f04a1fb3008--