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