From: "Volkmann, Mark" Date: 2003-08-07T03:03:49+09:00 Subject: Re: adding methods to an existing class This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C35C44.88084C04 Content-Type: text/plain; charset="iso-8859-1" Thanks! That did what I needed. Now I need to determine the name of the method that is currently running so I can do something like puts "The method #{what-goes-here} is currently running." Is there a way to get that? > -----Original Message----- > From: dblack@superlink.net [mailto:dblack@superlink.net] > Sent: Wednesday, August 06, 2003 11:31 AM > To: ruby-talk@ruby-lang.org > Subject: Re: adding methods to an existing class > > > Hi -- > > > On Thu, 7 Aug 2003, Volkmann, Mark wrote: > > > I'm fumbling over syntax trying to figure out how I could > write a method > > that adds a method to a given class. I'd call it like this. > > > > addTestMethod(Foo) > > > > where Foo is a class. > > > > addTestMethod would add a fixed "test" method to the class > passed to it. > > > > An example would be greatly appreciated! > > def add_test_method(klass) > klass.class_eval { > define_method(:some_test) { puts "testing!" } > } > # or: klass.class_eval "def some_test; puts \"testing!\"; end" > # or (1.8.0 only): > # klass.class_eval { def some_test; puts "testing!"; end } > end > > class A > end > > a = A.new > add_test_method(A) > a.some_test # => testing! > > (I've chosen "some_test" so as not to interfere with Kernel#test.) > > > David > > -- > David Alan Black > home: dblack@superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > ------------------------------------------------------------------------------------------------------------------- A.G. Edwards & Sons' outgoing and incoming e-mails are electronically archived and subject to review and/or disclosure to someone other than the recipient. ------------------------------------------------------------------------------------------------------------------- ------_=_NextPart_001_01C35C44.88084C04 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: adding methods to an existing class

Thanks!  That did what I needed.  Now I need to= determine the name of the method that is currently running so I can do som= ething like

puts "The method #{what-goes-here} is currently runn= ing."

Is there a way to get that?

> -----Original Message-----
> From: dblack@superlink.net [mailto:dblack@superlink.net]
> Sent: Wednesday, August 06, 2003 11:31 AM
> To: ruby-talk@ruby-lang.org
> Subject: Re: adding methods to an existing class
>
>
> Hi --
>
>
> On Thu, 7 Aug 2003, Volkmann, Mark wrote:
>
> > I'm fumbling over syntax trying to figure out = how I could
> write a method
> > that adds a method to a given class.  I'd= call it like this.
> >
> > addTestMethod(Foo)
> >
> > where Foo is a class.
> >
> > addTestMethod would add a fixed "test&quo= t; method to the class
> passed to it.
> >
> > An example would be greatly appreciated!
>
>   def add_test_method(klass)
>     klass.class_eval {
>       define_method(:= some_test) { puts "testing!" }
>     }
>   # or: klass.class_eval "def some_t= est; puts \"testing!\"; end"
>   # or (1.8.0 only):
>   #     klass.class_e= val { def some_test; puts "testing!"; end }
>   end
>
>   class A
>   end
>
>   a =3D A.new
>   add_test_method(A)
>   a.some_test    &nbs= p; # =3D>  testing!
>
> (I've chosen "some_test" so as not to int= erfere with Kernel#test.)
>
>
> David
>
> --
> David Alan Black
> home: dblack@superlink.net
> work: blackdav@shu.edu
> Web:  http://pirate.shu.edu/~blackdav
>
>



---------------------------------------------------------------------------= ----------------------------------------
A.G. Edwards & Sons' outgoing and incoming e-mails are electronically
archived and subject to review and/or disclosure to someone other
than the recipient.

---------------------------------------------------------------------------= ----------------------------------------
------_=_NextPart_001_01C35C44.88084C04--