# How to wrap method of singleton class

**URL:** <https://www.ruby-forum.com/t/how-to-wrap-method-of-singleton-class/153828>\
**Category:** Ruby\
**Created:** [November 21, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/how-to-wrap-method-of-singleton-class/153828 "2008-11-21T15:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [November 21, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/how-to-wrap-method-of-singleton-class/153828/1 "2008-11-21T15:44:30Z")

</div>

Hi,

Given a class:

class Foo

class \<\< self

```
def foo

  puts 'foo'

end

```

end

end

How do I write a module, so that when included in Foo it will wrap the  
method foo so that Foo.foo will print:

bar

foo

?

Thank you

Ittay

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [November 21, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/how-to-wrap-method-of-singleton-class/153828/2 "2008-11-21T16:22:41Z")

</div>

> How do I write a module, so that when included in Foo it will wrap the  
> method foo so that Foo.foo will print:

You can’t (at least not without doing some sneaky metaprogramming –  
this  
code is still a work in progress: [inherit\_from\_self.rb · GitHub](http://gist.github.com/25104)). A  
class’s  
own methods take precedence over any modules mixed into it. What you can  
do  
is reopen the singleton class and store a reference to the old method:

class \<\< Foo # Foo singleton class  
old\_foo = Foo.method(:foo)  
define\_method(:foo) do |\*args|  
old\_result = old\_foo.call(\*args)  
old\_result.upcase  
end  
end

Foo.foo #=\> “FOO”

You must use define\_method for this instead of def. def does not use a  
block  
so you won’t get a closure, which you need to retain visibility of the  
old\_foo variable.

---

<div class="post-metadata">

**Author:** ![ittayd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/54ee81/32.png) [@ittayd](https://www.ruby-forum.com/u/ittayd)\
**Post date:** [November 21, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/how-to-wrap-method-of-singleton-class/153828/3 "2008-11-21T20:45:00Z")

</div>

James C. wrote:

> class \<\< Foo # Foo singleton class  
> old\_foo = Foo.method(:foo)  
> define\_method(:foo) do |\*args|  
> old\_result = old\_foo.call(\*args)  
> old\_result.upcase  
> end  
> end

doing it this way, i can do:  
class \<\< Foo  
alias\_method :original, :foo  
def foo  
puts ‘bar’  
original  
end  
end

but i want a module i can mix in. (for regular methods, the module can  
use the included callback to alias the methods, just not sure how to do  
that for a singleton class)

> Foo.foo #=\> “FOO”
> 
> You must use define\_method for this instead of def. def does not use a block  
> so you won’t get a closure, which you need to retain visibility of the  
> old\_foo variable.

–  
Ittay D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Tikal [http://www.tikalk.com](http://www.tikalk.com)  
Tikal Project [http://tikal.sourceforge.net](http://tikal.sourceforge.net)
