`pp` Line Number & Filename Option?

I sometimes have several pp stragglers floating around and hate tracking them down. Is it possible to monkey patch the pp macro so it prefixes the output with a line number and filename?

GameFunctions.cr:

pp "Hello"
pp "Hello"
pp "Hello"
[1:GameFunctions.cr]: "Hello"
[2:GameFunctions.cr]: "Hello"
[3:GameFunctions.cr]: "Hello"

So I don’t recommend monkey patching pp, but it would be pretty easy to write a macro to do what you want. All macro types inherit from ASTNode, and ASTNode includes line_number and column_number methods. So:

macro puts_with_line(arg)
  puts "[{{arg.line_number.id}}:{{arg.filename.id}}] {{arg.id}}"
end

puts_with_line "Hello"
# => [5:eval] Hello
7 Likes

Damn! @watzon that’s awesome, thank you! It’s perfect

This library by @Sija does what you want too. It’s awesome!

2 Likes

Nice, @stakach. Starred that repo instantly