Top Level Namespace

Constant Summary collapse

STACK_TRACE_CALL_LINE_NUMBER_REGEX =
/\:(\d+)\:in /
STACK_TRACE_CALL_SOURCE_FILE_REGEX =
/[ ]*([^:]+)\:\d+\:in /

Instance Method Summary collapse

Instance Method Details

#__caller_line_number__(caller_depth = 0) ⇒ Object

Provides caller line number starting 1 level above caller of this method.

Example: “‘ruby puts “Print out caller_line_number” # line 1 puts caller_line_number # line 2 “` prints out 2



23
24
25
# File 'lib/puts_debuggerer.rb', line 23

def __caller_line_number__(caller_depth=0)
  caller[caller_depth][STACK_TRACE_CALL_LINE_NUMBER_REGEX, 1].to_i
end

#__caller_source_line__(caller_depth = 0) ⇒ Object

Provides caller source line starting 1 level above caller of this method.

Example: “‘ruby puts “Print out caller_line_number” # line 1 puts caller_line_number # line 2 “` prints out 2



37
38
39
40
41
42
43
44
45
46
# File 'lib/puts_debuggerer.rb', line 37

def __caller_source_line__(caller_depth=0)
  source_line_number = __caller_line_number__(caller_depth+1)
  source_file = caller[caller_depth][STACK_TRACE_CALL_SOURCE_FILE_REGEX, 1]
  source_line = nil
  File.open(source_file, 'r') do |f|
    lines = f.readlines
    source_line = lines[source_line_number-1]
  end
  source_line
end

#pd(object) ⇒ Object



1
2
3
4
5
6
7
8
9
# File 'lib/puts_debuggerer.rb', line 1

def pd(object)
  source_line = __caller_source_line__(1).strip.sub(/^[ ]*pd[ ]+/, '').gsub(/(^'|'$)/, '"')
  if source_line == object.inspect.sub("\n$", '')
    source_line = ''
  else
    source_line += '.inspect => '
  end
  puts "#{__caller_line_number__(1)}: #{source_line}#{object.inspect}"
end