The Crystal Programming Language Forum

`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?

pp "Hello"
pp "Hello"
pp "Hello"
[]: "Hello"
[]: "Hello"
[]: "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 "[{{}}:{{}}] {{}}"

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

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

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

1 Like

Nice, @stakach. Starred that repo instantly