Class: QQ

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
lib/qq.rb

Overview

QQ improves puts debugging.

All output goes to ‘q’ in your ‘Dir.tempdir`, normally ’/tmp/q’.

touch /tmp/q && tail -f /tmp/q

To print the value of something require ‘qq’ and use qq() anywhere you would have previously used pp(), puts etc and searched log files, $stderr, $stdout etc. for your debugging.

– TODO: Calling Q twice on the same line will cause issues because Thread::Backtrace::Location doesn’t give us a character only a line.

Examples:

require 'qq'; qq('hello world')

See Also:

  • https://github.com/zestyping/q
  • https://github.com/y0ssar1an/q

Instance Method Summary collapse

Constructor Details

#initialize(location, args) ⇒ QQ

– TODO: Complain if called directly.

See Also:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/qq.rb', line 43

def initialize location, args
  @location, @args = location, args
  @@mutex.synchronize do
    begin
      # Parse the statement that generated the argument from source.
      process(Parser::CurrentRuby.parse(File.read(location.absolute_path)))
    rescue StandardError
      # Failed to parse or embedded Ruby (HAML, ERB, ...) prints the position of each argument in qq()
      # location preamble/header.
      # line:0 arg:0 = ...
      # line:0 arg:1 = ...
      write args.each_with_index.map{|arg, position| [arg, 'line:%d arg:%d' % [@location.lineno, position]]}
    end
  end
end

Instance Method Details

#on_send(ast_node) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/qq.rb', line 59

def on_send ast_node
  return unless ast_node.loc.line == @location.lineno
  ast_receiver, ast_method, *ast_args = *ast_node

  return if ast_receiver || ast_method != :qq
  write @args.zip(ast_args).map{|arg, ast_arg| [arg, ast_arg.loc.expression.source]}
end