Hi --

On Mon, 11 Dec 2006, 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?

I reply under mild protest, as I detest the term "monkey patching"
(and never know what people mean by it, since they mean different
things).  Anyway... :-)  One way to change a method temporarily,
albeit a non-thread-safe way, is with aliases -- something like:

   alias newname oldname
   def oldname
     ...
   end
   alias oldname newname

That was the basis of Ruby Behaviors, a package I wrote in 2001 to do
exactly this: temporary changes to core behaviors.  Matz described the
alias approach at RubyConf 2001 as a "naive" way to go about it :-)
It did however spark an interesting discussion about selector
namespaces, a discussion we're basically still having.

There are, or were, also some other libraries on RAA that address
this.  I'm afraid I don't remember their names and am being too lazy
to look them up, but if you hunt for library stuff pertaining to
classes and methods you'll most likely find them.


David

-- 
                   David A. Black | dblack / wobblini.net
Author of "Ruby for Rails"   [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog)        [2] | Co-director, Ruby Central, Inc.   [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com    | [4] http://www.rubycentral.org