From: Zach Dennis Date: 2005-02-10T07:02:13+09:00 Subject: Re: redefine Kernel method koboi wrote: > I would like to redefine the method Kernel#rand inside a test method, and > reset it in the end again. > But everything I try fails. If I redefine it outside my module and class, > it works. But I would like to do it inside my test method. > def meth m = proc { 5 } Kernel.module_eval( "alias :rand_old :rand" ) Kernel.module_eval( "def rand; #{m.call}; end" ) puts rand Kernel.module_eval( "alias :rand :rand_old" ) Kernel.module_eval( "remove_method( :rand_old ) " ) end meth puts rand I hope this moves you in the right direction, Zach