# Best praxis to wrap methods?

**URL:** <https://www.ruby-forum.com/t/best-praxis-to-wrap-methods/54507>\
**Category:** Ruby\
**Created:** [March 7, 2006, 10:42am UTC](https://www.ruby-forum.com/t/best-praxis-to-wrap-methods/54507 "2006-03-07T10:42:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [March 7, 2006, 10:42am UTC](https://www.ruby-forum.com/t/best-praxis-to-wrap-methods/54507/1 "2006-03-07T10:42:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [March 7, 2006, 10:45am UTC](https://www.ruby-forum.com/t/best-praxis-to-wrap-methods/54507/2 "2006-03-07T10:45:15Z")

</div>

On 3/7/06, Meinrad R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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:

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [March 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/best-praxis-to-wrap-methods/54507/3 "2006-03-07T10:51:18Z")

</div>

Meinrad R. wrote:

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

---

<div class="post-metadata">

**Author:** ![Meinrad\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@Meinrad\_R](https://www.ruby-forum.com/u/Meinrad_R)\
**Post date:** [March 7, 2006, 11:03am UTC](https://www.ruby-forum.com/t/best-praxis-to-wrap-methods/54507/4 "2006-03-07T11:03:33Z")

</div>

On 3/7/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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