[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Monkey Patching a method and back

S. Robert James

12/10/2006 9:34:00 PM

I need to temporarily mokey patch out a method in a certain class used
by a lib in one of my tests. That's easy.

However, what I don't know how to do is to restore back the orginial
(without copying in the src, of course), when I'm done. Is there any
good way to do this - that is, monkey patch temporarily, and then put
things back the way they were?

9 Answers

James Gray

12/10/2006 9:43:00 PM

0

On Dec 10, 2006, at 3:35 PM, S. Robert James wrote:

> I need to temporarily mokey patch out a method in a certain class used
> by a lib in one of my tests. That's easy.
>
> However, what I don't know how to do is to restore back the orginial
> (without copying in the src, of course), when I'm done. Is there any
> good way to do this - that is, monkey patch temporarily, and then put
> things back the way they were?

Sure:

#!/usr/bin/env ruby -w

class Existing
def needs_patching
:old
end
end
e = Existing.new
e.needs_patching # => :old

# save the old and patch
class Existing
alias_method :saved_needs_patching, :needs_patching
def needs_patching
:new
end
end
e.needs_patching # => :new

# restore the old
class Existing
undef :needs_patching
alias_method :needs_patching, :saved_needs_patching
end
e.needs_patching # => :old

__END__

Hope that helps.

James Edward Gray II


dblack

12/10/2006 9:49:00 PM

0

dblack

12/10/2006 9:56:00 PM

0

S. Robert James

12/10/2006 10:01:00 PM

0

Will this be OK even if patched and unpatched multiple times?


James Edward Gray II wrote:
> On Dec 10, 2006, at 3:35 PM, S. Robert James wrote:
>
> > I need to temporarily mokey patch out a method in a certain class used
> > by a lib in one of my tests. That's easy.
> >
> > However, what I don't know how to do is to restore back the orginial
> > (without copying in the src, of course), when I'm done. Is there any
> > good way to do this - that is, monkey patch temporarily, and then put
> > things back the way they were?
>
> Sure:
>
> #!/usr/bin/env ruby -w
>
> class Existing
> def needs_patching
> :old
> end
> end
> e = Existing.new
> e.needs_patching # => :old
>
> # save the old and patch
> class Existing
> alias_method :saved_needs_patching, :needs_patching
> def needs_patching
> :new
> end
> end
> e.needs_patching # => :new
>
> # restore the old
> class Existing
> undef :needs_patching
> alias_method :needs_patching, :saved_needs_patching
> end
> e.needs_patching # => :old
>
> __END__
>
> Hope that helps.
>
> James Edward Gray II

James Gray

12/10/2006 10:09:00 PM

0

On Dec 10, 2006, at 4:05 PM, S. Robert James wrote:

> Will this be OK even if patched and unpatched multiple times?

You probably want to use something more robust for that. You need to
make sure you only alias the method is a saved version doesn't
already exist.

James Edward Gray II


dblack

12/10/2006 10:15:00 PM

0

S. Robert James

12/11/2006 1:42:00 AM

0


dblack@wobblini.net wrote:
> In case it got lost in my reply to my reply: have a look at:
>
> http://raa.ruby-lang.org/project/impo...

Thanks. That's quite a hefty module - with method am I interested in?

dblack

12/11/2006 2:49:00 AM

0

Martin DeMello

12/11/2006 11:04:00 AM

0

On 12/11/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
>
> I reply under mild protest, as I detest the term "monkey patching"

+1

martin