Professional Web Applications Themes

adding methods to an existing class - Ruby

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

  1. #1

    Default 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: [email]dblacksuperlink.net[/email]
    work: [email]blackdavshu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  2. #2

    Default 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; cht="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: [email]dblacksuperlink.net[/email] [mailto:dblacksuperlink.net]
    > Sent: Wednesday, August 06, 2003 11:31 AM
    > To: [email]ruby-talkruby-lang.org[/email]
    > 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: [email]dblacksuperlink.net[/email]
    > work: [email]blackdavshu.edu[/email]
    > Web: [url]http://pirate.shu.edu/~blackdav[/url]
    >
    >

    -------------------------------------------------------------------------------------------------------------------
    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; cht="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <HTML>
    <HEAD>
    <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; cht=3Diso-8859-=
    1">
    <META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version 5.5.2655.35">
    <TITLE>RE: adding methods to an existing class</TITLE>
    </HEAD>
    <BODY>

    <P><FONT SIZE=3D2>Thanks!&nbsp; That did what I needed.&nbsp; Now I need to=
    determine the name of the method that is currently running so I can do som=
    ething like</FONT></P>

    <P><FONT SIZE=3D2>puts &quot;The method #{what-goes-here} is currently runn=
    ing.&quot;</FONT>
    </P>

    <P><FONT SIZE=3D2>Is there a way to get that?</FONT>
    </P>

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

    <CODE><FONT SIZE=3D3><BR>
    <BR>
    ---------------------------------------------------------------------------=
    ----------------------------------------<BR>
    A.G. Edwards & Sons' outgoing and incoming e-mails are electronically<BR>
    archived and subject to review and/or disclosure to someone other <BR>
    than the recipient.<BR>
    <BR>
    ---------------------------------------------------------------------------=
    ----------------------------------------<BR>
    </FONT></CODE></BODY>
    </HTML>
    ------_=_NextPart_001_01C35C44.88084C04--

    Volkmann, Mark Guest

Similar Threads

  1. Adding new text to existing PDF
    By Pat_Dohne@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 17
    Last Post: August 27th, 07:09 PM
  2. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 2
    Last Post: October 3rd, 03:52 AM
  3. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 04:19 PM
  4. Instance- and class-variables (was mixing in class methods)
    By dblack@superlink.net in forum Ruby
    Replies: 0
    Last Post: October 2nd, 12:46 PM
  5. Replies: 1
    Last Post: August 18th, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139