best praxis to wrap methods?

Discussion in 'Ruby' started by Meinrad Recheis, Mar 7, 2006.

  1. ------=_Part_1901_6993034.1141724373102
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    hello fellow rubyists,

    is there an elegant way to define a singleton method from a given symbol
    without using eval? what i am doing is this:

    def make_wrapper object, method
    eval %{
    def object.#{method}
    # do something here
    super # call the wrapped method
    end
    }
    end

    # example of usage:
    make_wrapper STDOUT, :puts

    using eval works, but it seems not to be the best solution. is there
    annother way do define methods from a given symbol?

    -- henon

    ------=_Part_1901_6993034.1141724373102--
     
    Meinrad Recheis, Mar 7, 2006
    #1
    1. Advertising

  2. ------=_Part_1929_31062276.1141724577777
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 3/7/06, Meinrad Recheis <> wrote:
    >
    > hello fellow rubyists,
    >
    > is there an elegant way to define a singleton method from a given symbol
    > without using eval? what i am doing is this:
    >
    > def make_wrapper object, method
    > eval %{
    > def object.#{method}
    > # do something here
    > super # call the wrapped method
    > end
    > }
    > end



    sorry the above code does not work, (needs to be done using
    object.instance_eval) but you get the idea what i mean

    # example of usage:
    > make_wrapper STDOUT, :puts
    >
    > using eval works, but it seems not to be the best solution. is there
    > annother way do define methods from a given symbol?
    >
    > -- henon
    >
    >


    ------=_Part_1929_31062276.1141724577777--
     
    Meinrad Recheis, Mar 7, 2006
    #2
    1. Advertising

  3. Meinrad Recheis wrote:
    > hello fellow rubyists,
    >
    > is there an elegant way to define a singleton method from a given
    > symbol without using eval? what i am doing is this:
    >
    > def make_wrapper object, method
    > eval %{
    > def object.#{method}
    > # do something here
    > super # call the wrapped method
    > end
    > }
    > end
    >
    > # example of usage:
    > make_wrapper STDOUT, :puts
    >
    > using eval works, but it seems not to be the best solution. is there
    > annother way do define methods from a given symbol?


    Are you sure the code you presented actually works? IMHO your code will
    fail if the method is defined in the same class (i.e. no "super"
    possible).

    You could use Delegator for this. And, if the method you want to wrap
    does not use a block define_method usually works quite well.

    Kind regards

    robert
     
    Robert Klemme, Mar 7, 2006
    #3
  4. ------=_Part_2093_987305.1141725759967
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 3/7/06, Robert Klemme <> wrote:
    >
    > Meinrad Recheis wrote:
    > [snipped]
    >
    > Are you sure the code you presented actually works? IMHO your code will
    > fail if the method is defined in the same class (i.e. no "super"
    > possible).



    afaik calling super in a singleton method calls the overridden method of
    that object (of course i assert that the method to be wrapped exists).
    here is the working code snippet:
    def make_wrapper object, method
    object.instance_eval %{
    def self.#{method}(*args, &block)
    puts "wrapped method #{method}"
    super
    end
    }
    end

    You could use Delegator for this. And, if the method you want to wrap
    > does not use a block define_method usually works quite well.



    ah, object.send( :define_method) is what i was looking for. thanks

    Kind regards
    >
    > robert
    >
    >
    >


    ------=_Part_2093_987305.1141725759967--
     
    Meinrad Recheis, Mar 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    742
    Chronic Philharmonic
    May 10, 2008
  2. Phil Bewig

    Programming Praxis

    Phil Bewig, May 29, 2009, in forum: Python
    Replies:
    0
    Views:
    270
    Phil Bewig
    May 29, 2009
  3. Phil Bewig

    Programming Praxis

    Phil Bewig, May 29, 2009, in forum: Python
    Replies:
    0
    Views:
    302
    Phil Bewig
    May 29, 2009
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    276
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    281
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page