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