Module: Kernel
- Defined in:
- lib/puts_debuggerer/core_ext/kernel.rb
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_line_count = nil, source_file = nil, source_line_number = nil) ⇒ Object
Provides caller source line starting 1 level above caller of this method.
- #caller(*args) ⇒ Object
-
#pd(*objects) ⇒ Object
Prints object with bonus info such as file name, line number and source expression.
- #pd_inspect ⇒ Object (also: #pdi)
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`
141 142 143 144 |
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 141 def __caller_file__(caller_depth=0) regex = RUBY_ENGINE == '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`
127 128 129 130 |
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 127 def __caller_line_number__(caller_depth=0) return if RUBY_ENGINE == '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`
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 155 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 defined?(Pry) @pry_instance ||= Pry.new source_line = Pry::Command::Hist.new(pry_instance: @pry_instance).call.instance_variable_get(:@buffer).split("\n")[source_line_number - 1] # TODO handle multi-lines in source_line_count elsif defined?(IRB) && TOPLEVEL_BINDING.receiver.respond_to?(:conf) source_line = TOPLEVEL_BINDING.receiver.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 |
#caller(*args) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 91 def caller(*args) dup_args = args.dup start = args.shift if args.first.is_a?(Integer) length = args.shift if args.first.is_a?(Integer) range = args.shift if args.first.is_a?(Range) if range start = range.begin length = range.end - start end begin raise 'error' rescue => e the_backtrace = e.backtrace start ||= 0 start = 2 + start length ||= the_backtrace.size - start the_backtrace[start, length] 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 84 85 86 87 |
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 33 def pd(*objects) = PutsDebuggerer.(objects) || {} object = PutsDebuggerer.determine_object(objects) run_at = PutsDebuggerer.determine_run_at() printer = PutsDebuggerer.determine_printer() pd_inspect = .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) () do || run_number = PutsDebuggerer::RunDeterminer.run_number(object, run_at) formatter_pd_data = __build_pd_data__(object, 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 RUBY_ENGINE == 'opal' && object.is_a?(Exception) $stderr.puts(string) else 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 end printer ? object : string end |
#pd_inspect ⇒ Object Also known as: pdi
112 113 114 |
# File 'lib/puts_debuggerer/core_ext/kernel.rb', line 112 def pd_inspect pd self, printer: false, pd_inspect: true end |