Here is a simple technique where you can inject mock methods into a class that you are unit testing.
Given you have a class classA
You can replace the "hi" method from your unit test, without editing the source code of ClassA at all. You can inject mock methods - whatever.
The trouble with class_eval is that it alters the class permanently, which is not so good for repeated unit testing where you may be injecting different things in different tests, or not injecting anything at all in some tests etc. I couldn’t figure out how to restore a class in setup/teardown so....instance_eval is probably better as it only affects an instance. Viz: