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.
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.
It moves in the right direction, yes, thanks.
But still, it doesn't work as I would like. If I want to count, how many
times the method is called, the Proc is evaluated only once, so the count
is always 1.
Example: @count = 0
m = proc { @count += 1; 5 }
#... #call rand several times
p @count -> 1
It moves in the right direction, yes, thanks.
But still, it doesn't work as I would like. If I want to count, how many
times the method is called, the Proc is evaluated only once, so the count
is always 1.
Example: @count = 0
m = proc { @count += 1; 5 }
#... #call rand several times
p @count -> 1
I'm going to repost after i figure this one out, the problem with my first post (and i apologize for posting inaccurate code), was that the "#{m.call}" was getting evaluated in the 'module_eval( "..." )' and it wasn't actually getting called when rand got called.
Kernel.module_eval( "def rand; #{m.call}; end" )
Will have rand return the value of m.call. It will always return the same thing, because m.call is evaluated in the String, and not when rand is called.