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
-
#__caller_file__(caller_depth = 0) ⇒ Object
Provides caller file starting 1 level above caller of this method.
-
#__caller_line_number__(caller_depth = 0) ⇒ Object
Provides caller line number starting 1 level above caller of this method.
-
#__caller_source_line__(caller_depth = 0, source_file = nil, source_line_number = nil) ⇒ Object
Provides caller source line starting 1 level above caller of this method.
-
#pd(object, options = nil) ⇒ Object
Prints object with bonus info such as file name, line number and source expression.
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`
344 345 346 |
# File 'lib/puts_debuggerer.rb', line 344 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`
331 332 333 |
# File 'lib/puts_debuggerer.rb', line 331 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, source_file = nil, source_line_number = nil) ⇒ Object
Provides caller source line starting 1 level above caller of this method.
Example:
puts __caller_source_line__
prints out ‘puts caller_source_line`
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/puts_debuggerer.rb', line 357 def __caller_source_line__(caller_depth=0, source_file=nil, source_line_number=nil) 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.include?('%>') #the last condition is for erb support (unofficial) 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?"
309 310 311 312 313 314 315 |
# File 'lib/puts_debuggerer.rb', line 309 def pd(object, =nil) () do || formatter_pd_data = __build_pd_data__(object, ) #depth adds build method PutsDebuggerer.formatter.call(formatter_pd_data) end object end |