ruby picture

RCR 255: Add trace info to Kernel#p

Submitted by batkins (Fri May 21 19:05:42 UTC 2004)


Many Ruby programmers use Kernel#p to examine an object's state for debugging purposes. However, p's output does not indicate the file and line from with p was called.


Since Kernel#p doesn't tell the user where it was executed in the file, it is slightly less useful for debugging. If Kernel#p supplied this information, then users of tools like SciTE could quickly jump to the location of output from Kernel#p.


Have Kernel#p output file and line information similar to exception traces. So "p str" would yield:

file.rb:18:"this is in str"


This could cause problems with older scripts that don't expect this to happen. For this reason, it might make sense to enable tracing only if $debug == true


module Kernel
  def p *o
    prefix = caller[0].split(/:/).values_at(0, 1).join(":") + ":"
    # do whatever p does here, but prepend 
    # variable prefix to the output
ruby picture
Comments Current voting
Strongly opposed 0
Opposed 6
Neutral 0
In favor 1
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .