Cutter

Two-methods-gem I use a lot for simple debugging & performance measuring purposes.

#inspect method shines when doing reverse engineering, it is especially useful, when it is needed to quickly hack on someone else's code. Also, it is very easy to become 'someone else' for self, when dealing with own code, if it was written very long time ago.

Besides that #stamper allows doing performance measuments in a handy manner, it can be used to create quick and neat demonstrations of how particular pieces of Ruby code perform.

The one interesting possible usage of #stamper is performance optimization of templates on Rails View Layer, because it often takes a large load impact (compared to M and C layers) because of Rails lazy-evaluation mechanisms.

Stamper with named stamps

puts"Now with named stamps"Cutter::Stamper.scope:testing_method=>"Demonstration of named stamping"do|tm|tm.msg_1:"first piece"tm.msg_2:"second piece"endCutter::Stamper.scope:inner_scope=>"Now internal things"do|i|i.msgfirst:"I'm the first inner stamp"endstamper:testing_methoddo|tm|sleep0.3tm.stamp!:_1# The old form of calling #stamp! on yielded scopevariablesleep0.3stamper:inner_scopedo|i|sleep0.2i.stamp!:firstsleep0.2i.stamp!"Stamp with custom text"endtm.stamp!:_2end

Stamper with :capture => true option

Use it to hide the output of piece you are benchmarking.

require'cutter'N=100000result=[]EMB="String to embed"result<<stamper(:capture=>true)doN.timesdoputs"#{EMB}\n"endendresult<<stamper(:capture=>true)doN.timesdoprintf"#{EMB}\n"endendresult<<stamper(:capture=>true)doN.timesdoprint"#{EMB}\n"endendputsresult.inspect

Notes

Both #inspect! {} and #stamper methods colorize their output. You can see lib/cutter/colored_output.rb file to understand how it is done. I will really appreciate any suggestions of how current color scheme can be improved.