class QQ

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.

@example

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

@see Python github.com/zestyping/q @see Go github.com/y0ssar1an/q

Constants

CYAN

Public Class Methods

new(location, args) click to toggle source

@see Kernel#qq

# 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

Public Instance Methods

on_send(ast_node) click to toggle source
# 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

Protected Instance Methods

write(args) click to toggle source
# File lib/qq.rb, line 69
def write args
  File.open(File.join(Dir.tmpdir, 'q'), 'a') do |fh|
    now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)

    if @@start <= now - 2.0 || @@location&.label != @location.label
      fh.write "\n%s[%s] %s\n" % [NORMAL, Time.now.strftime('%T'), @location]
      @@start = now
    end

    args.each do |arg, arg_source|
      fh.write [YELLOW, '%1.3fs ' % (now - @@start), NORMAL, arg_source, ' = ', CYAN].join
      PP.pp(arg, fh)
      fh.write NORMAL
    end

    @@location = @location
  end
end