Module: Kernel

Defined in:
lib/puts_debuggerer/core_ext/kernel.rb

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`



125
126
127
128
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 125

def __caller_file__(caller_depth=0)
  regex = RUBY_PLATFORM == 'opal' ? PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX_OPAL : PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX
  caller[caller_depth] && caller[caller_depth][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`



111
112
113
114
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 111

def __caller_line_number__(caller_depth=0)
  return if RUBY_PLATFORM == 'opal'
  caller[caller_depth] && caller[caller_depth][PutsDebuggerer::STACK_TRACE_CALL_LINE_NUMBER_REGEX, 1].to_i
end

#__caller_source_line__(caller_depth = 0, source_line_count = nil, 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`



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 139

def __caller_source_line__(caller_depth=0, source_line_count=nil, 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 = ''
  if source_file == '(irb)'
    source_line = conf.io.line(source_line_number) # TODO handle multi-lines in source_line_count
  else
    source_line = PutsDebuggerer::SourceFile.new(source_file).source(source_line_count, source_line_number)
  end
  source_line
end

#callerObject



87
88
89
90
91
92
93
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 87

def caller
  begin
    raise 'error'
  rescue => e
    e.backtrace[2..-1]
  end
end

#pd(*objects) ⇒ 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?"


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 33

def pd(*objects)
  options = PutsDebuggerer.determine_options(objects) || {}
  object = PutsDebuggerer.determine_object(objects)
  run_at = PutsDebuggerer.determine_run_at(options)
  printer = PutsDebuggerer.determine_printer(options)
  pd_inspect = options.delete(:pd_inspect)
  logger_formatter_decorated = PutsDebuggerer.printer.is_a?(Logger) && PutsDebuggerer.printer.formatter != PutsDebuggerer.logger_original_formatter
  logging_layouts_decorated = PutsDebuggerer.printer.is_a?(Logging::Logger) && PutsDebuggerer.printer.appenders.map(&:layout) != (PutsDebuggerer.logging_original_layouts.values)

  string = nil
  if PutsDebuggerer::RunDeterminer.run_pd?(object, run_at)
    __with_pd_options__(options) do |print_engine_options|
      run_number = PutsDebuggerer::RunDeterminer.run_number(object, run_at)
      formatter_pd_data = __build_pd_data__(object, print_engine_options: print_engine_options, source_line_count: PutsDebuggerer.source_line_count, run_number: run_number, pd_inspect: pd_inspect, logger_formatter_decorated: logger_formatter_decorated, logging_layouts_decorated: logging_layouts_decorated)
      stdout = $stdout
      $stdout = sio = StringIO.new
      PutsDebuggerer.formatter.call(formatter_pd_data)
      $stdout = stdout
      string = sio.string
      if PutsDebuggerer.printer.is_a?(Proc)
        PutsDebuggerer.printer.call(string)
      elsif PutsDebuggerer.printer.is_a?(Logger)
        logger_formatter = PutsDebuggerer.printer.formatter
        begin
          PutsDebuggerer.printer.formatter = PutsDebuggerer.logger_original_formatter
          PutsDebuggerer.printer.debug(string)
        ensure
          PutsDebuggerer.printer.formatter = logger_formatter
        end
      elsif PutsDebuggerer.printer.is_a?(Logging::Logger)
        logging_layouts = PutsDebuggerer.printer.appenders.reduce({}) do |hash, appender|
          hash.merge(appender => appender.layout)
        end
        begin
          PutsDebuggerer.logging_original_layouts.each do |appender, original_layout|
            appender.layout = original_layout
          end
          PutsDebuggerer.printer.debug(string)
        ensure
          PutsDebuggerer.logging_original_layouts.each do |appender, original_layout|
            appender.layout = logging_layouts[appender]
          end
        end        
      elsif PutsDebuggerer.printer != false
        send(PutsDebuggerer.send(:printer), string)
      end
    end
  end

  printer ? object : string
end

#pd_inspectObject Also known as: pdi



96
97
98
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 96

def pd_inspect
  pd self, printer: false, pd_inspect: true
end