# Monkeypatching multiple classes with similar methods

**URL:** <https://www.ruby-forum.com/t/monkeypatching-multiple-classes-with-similar-methods/175892>\
**Category:** Ruby\
**Created:** [September 22, 2009, 10:45am UTC](https://www.ruby-forum.com/t/monkeypatching-multiple-classes-with-similar-methods/175892 "2009-09-22T10:45:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/91b2a8/32.png) [@Lars\_O](https://www.ruby-forum.com/u/Lars_O)\
**Post date:** [September 22, 2009, 10:45am UTC](https://www.ruby-forum.com/t/monkeypatching-multiple-classes-with-similar-methods/175892/1 "2009-09-22T10:45:36Z")

</div>

Hello!

I’m trying to monkeypatch a number of related classes that all  
implements the same method (meth). The methods all take one required  
argument (stuff) but some of the individual methods may require more  
arguments (or a block). All I want to do in each call to meth is to  
print some debug data and then pass the incoming arguments to the  
original method. This works great as long as I know the exact number  
of arguments required, but when the number of arguments starts to  
change my code breaks badly. Is there any way (in ruby 1.8.7) to  
“copy” the original method signature when defining my own replacement  
method?

/lasso

— CODE START —  
class Klass1  
def meth(stuff)  
puts “You’ve sent #{stuff} to #{self.class}.meth”  
end  
end

class Klass2  
def meth(stuff, &block)  
block\_text = block\_given? ? ‘with’ : ‘without’  
puts “You’ve sent #{stuff} to #{self.class}.meth #{block\_text} a  
block”  
end  
end

class Klass3  
def meth(stuff, morestuff)  
puts “You’ve sent #{stuff} to #{self.class}.meth”  
end  
end

# Broken monkeypatching. Only works on Klass1

[Klass1, Klass2, Klass3].each do |klass|  
klass.module\_eval do  
alias\_method :original\_meth, :meth  
def meth(stuff)  
puts “#{self.class}.meth has been patched”  
original\_meth(stuff)  
end  
end  
end

Klass1.new.meth(‘one’)  
Klass2.new.meth(‘two’) { ‘three’ } # Block is not passed after  
monkeypatching  
Klass2.new.meth(‘four’)  
Klass3.new.meth(‘five’, ‘six’) # Raises ArgumentError after  
monkeypatching  
— CODE END —

---

<div class="post-metadata">

**Author:** ![Lars\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/91b2a8/32.png) [@Lars\_O](https://www.ruby-forum.com/u/Lars_O)\
**Post date:** [September 22, 2009, 10:56am UTC](https://www.ruby-forum.com/t/monkeypatching-multiple-classes-with-similar-methods/175892/2 "2009-09-22T10:56:43Z")

</div>

Hey. You just have a tiny couple of bugs. Try this instead (untested,  
but you get the idea):

[Klass1, Klass2, Klass3].each do |klass|  
klass.module\_eval do  
alias\_method :original\_meth, :meth  
def meth(\*stuff, &block)  
puts “#{self.class}.meth has been patched”  
original\_meth(\*stuff, &block)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Lars\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/91b2a8/32.png) [@Lars\_O](https://www.ruby-forum.com/u/Lars_O)\
**Post date:** [September 22, 2009, 11:10am UTC](https://www.ruby-forum.com/t/monkeypatching-multiple-classes-with-similar-methods/175892/3 "2009-09-22T11:10:47Z")

</div>

On 22 Sep, 10:56, Ehsanul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> > Hello!  
> > method?  
> > class Klass2  
> > end  
> > end
> 
> * * *
> 
> Microsoft brings you a new way to search the web. Try Bing™ nowhttp://www.bing.com?form=MFEHPG&publ=WLHMTAG&crea=TEXT\_MFEHPG\_Core\_ta…bing\_1x1- Dölj citerad text -
> 
> - Visa citerad text -

Exactly what I needed (and even simpler than I though). Your solution  
works perfectly. Thank you!

/lasso

---

<div class="post-metadata">

**Author:** ![Lars\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/91b2a8/32.png) [@Lars\_O](https://www.ruby-forum.com/u/Lars_O)\
**Post date:** [September 22, 2009, 11:18am UTC](https://www.ruby-forum.com/t/monkeypatching-multiple-classes-with-similar-methods/175892/4 "2009-09-22T11:18:59Z")

</div>

> Exactly what I needed (and even simpler than I though). Your solution  
> works perfectly. Thank you!
> 
> /lasso

You’re welcome! I’m just glad to be of service to the ruby community  
(finally). =)
