Object.tap
オブジェクトの状態を任意の場所で確認できるため、便利。従来のデバッグ方法のように構文を大きく崩す事なく実行できるところが良い。
class Object def tap yield self self end end
使用例
blah.sort.grep( /foo/ ).tap { |xs| p xs }.map { |x| x.blah } ( k + 1 ) / ( ( q - t ).tap { |i| p i } / 2 ) def blah @things.map { |x| x.length }.inject( 0 ) { |a, b| a + b }.tap { |sum| p sum } end