Top Level Namespace

Defined Under Namespace

Modules: PutsDebuggerer

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_file__(caller_depth = 0) ⇒ Object

Provides caller file starting 1 level above caller of this method.

Example:

# File Name: lib/example.rb
puts __caller_file__

prints out ‘lib/example.rb`



335
336
337
# File 'lib/puts_debuggerer.rb', line 335

def __caller_file__(caller_depth=0)
  caller[caller_depth][STACK_TRACE_CALL_SOURCE_FILE_REGEX, 1]
end

#__caller_line_number__(caller_depth = 0) ⇒ Object

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

Example:

# lib/example.rb                        # line 1
puts "Print out __caller_line_number__" # line 2
puts __caller_line_number__             # line 3

prints out ‘3`



322
323
324
# File 'lib/puts_debuggerer.rb', line 322

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:

puts __caller_source_line__

prints out ‘puts caller_source_line`



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/puts_debuggerer.rb', line 348

def __caller_source_line__(caller_depth=0)
  source_line_number = __caller_line_number__(caller_depth+1)
  source_file = __caller_file__(caller_depth+1)
  source_line = nil
  if source_file == '(irb)'
    source_line = conf.io.line(source_line_number)
  else
    f = File.new(source_file)
    source_line = ''
    done = false
    f.each_line do |line|
      if !done && f.lineno == source_line_number
        source_line << line
        done = true if Ripper.sexp_raw(source_line)
        source_line_number+=1
      end
    end
  end
  source_line
end

#pd(object, options = nil) ⇒ Object

Prints object with bonus info such as file name, line number and source expression. Optionally prints out header and footer. Lookup PutsDebuggerer attributes for more details about configuration options.

Simply invoke global ‘pd` method anywhere you’d like to see line number and source code with output. If the argument is a pure string, the print out is simplified by not showing duplicate source.

Quickly locate printed lines using Find feature (e.g. CTRL+F) by looking for:

  • [PD]

  • file:line_number

  • ruby expression.

This gives you the added benefit of easily removing your pd statements later on from the code.

Happy puts_debuggerering!

Example Code:

# /Users/User/finance_calculator_app/pd_test.rb # line 1
bug = 'beattle'                                 # line 2
pd "Show me the source of the bug: #{bug}"      # line 3
pd 'What line number am I?'                     # line 4

Example Printout:

[PD] /Users/User/finance_calculator_app/pd_test.rb:3
   > pd "Show me the source of the bug: #{bug}"
  => "Show me the source of the bug: beattle"
[PD] /Users/User/finance_calculator_app/pd_test.rb:4 "What line number am I?"


300
301
302
303
304
305
306
# File 'lib/puts_debuggerer.rb', line 300

def pd(object, options=nil)
  __with_pd_options__(options) do |print_engine_options|
    formatter_pd_data = __build_pd_data__(object, print_engine_options) #depth adds build method
    PutsDebuggerer.formatter.call(formatter_pd_data)
  end
  nil
end