From: Gennady Date: 2003-08-07T03:07:48+09:00 Subject: Re: adding methods to an existing class This is a multi-part message in MIME format. ------=_NextPart_000_000A_01C35C0B.061C75F0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: adding methods to an existing classTake a look at Kernel::caller ----- Original Message -----=20 From: Volkmann, Mark=20 To: ruby-talk ML=20 Sent: Wednesday, August 06, 2003 11:03 AM Subject: 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 something like puts "The method #{what-goes-here} is currently running."=20 Is there a way to get that?=20 > -----Original Message-----=20 > From: dblack@superlink.net [mailto:dblack@superlink.net]=20 > Sent: Wednesday, August 06, 2003 11:31 AM=20 > To: ruby-talk@ruby-lang.org=20 > Subject: Re: adding methods to an existing class=20 >=20 >=20 > Hi --=20 >=20 >=20 > On Thu, 7 Aug 2003, Volkmann, Mark wrote:=20 >=20 > > I'm fumbling over syntax trying to figure out how I could=20 > write a method=20 > > that adds a method to a given class. I'd call it like this.=20 > >=20 > > addTestMethod(Foo)=20 > >=20 > > where Foo is a class.=20 > >=20 > > addTestMethod would add a fixed "test" method to the class=20 > passed to it.=20 > >=20 > > An example would be greatly appreciated!=20 >=20 > def add_test_method(klass)=20 > klass.class_eval {=20 > define_method(:some_test) { puts "testing!" }=20 > }=20 > # or: klass.class_eval "def some_test; puts \"testing!\"; end"=20 > # or (1.8.0 only):=20 > # klass.class_eval { def some_test; puts "testing!"; end }=20 > end=20 >=20 > class A=20 > end=20 >=20 > a =3D A.new=20 > add_test_method(A)=20 > a.some_test # =3D> testing!=20 >=20 > (I've chosen "some_test" so as not to interfere with Kernel#test.)=20 >=20 >=20 > David=20 >=20 > --=20 > David Alan Black=20 > home: dblack@superlink.net=20 > work: blackdav@shu.edu=20 > Web: http://pirate.shu.edu/~blackdav=20 >=20 >=20 = -------------------------------------------------------------------------= ------------------------------------------ A.G. Edwards & Sons' outgoing and incoming e-mails are electronically archived and subject to review and/or disclosure to someone other=20 than the recipient. = -------------------------------------------------------------------------= ------------------------------------------ ------=_NextPart_000_000A_01C35C0B.061C75F0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: adding methods to an existing class
Take a look at = Kernel::caller
----- Original Message -----
From:=20 Volkmann, Mark
To: ruby-talk ML
Sent: Wednesday, August 06, = 2003 11:03=20 AM
Subject: Re: adding methods to = an=20 existing class

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

puts "The method #{what-goes-here} is currently=20 running."

Is there a way to get that?

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



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

-------------------------------------------------------= ------------------------------------------------------------
------=_NextPart_000_000A_01C35C0B.061C75F0--