module Debugging

Constants

VERSION

Private Instance Methods

at(label = nil) click to toggle source
# File lib/debugging/at.rb, line 6
def at(label = nil)
  caller[0].rindex( /:(\d+)(:in (`.*'))?$/ )
  puts "#{ label && Paint["#{label} ", :yellow] }@"\
       " #{ $3 && "method #{ Paint[$3, :red] }, " }line "\
       "#{ Paint[$1, :blue]} of file #{ Paint[$`, :green]}"
end
beep() click to toggle source
# File lib/debugging/beep.rb, line 6
def beep
  $stdout.print "\a"
end
callstack() click to toggle source
# File lib/debugging/callstack.rb, line 6
def callstack
  caller.reverse.map{ |m|
    m.rindex( /:\d+(:in `(.*)')?$/ )
    $2
  }.compact.each.with_index{ |m, i|
    puts "  "*i + m
  }

  nil
end
howtocall(object = self, method_or_proc) click to toggle source
# File lib/debugging/howtocall.rb, line 6
def howtocall(object = self, method_or_proc)
  if method_or_proc.is_a? Proc
    params = method_or_proc.parameters
    template = "call(%s)"
  else
    unless method_or_proc.is_a?(Method) || method_or_proc.is_a?(UnboundMethod)
      method_or_proc = object.method(method_or_proc)
    end
    params = method_or_proc.parameters
    template = "#{method_or_proc.name}(%s)"
  end

  sig = params.map{ |type, name|
    param = ""
    param << "*" if type == :rest
    param << "**" if type == :keyrest
    param << "&" if type == :block
    name = ?? if !name && !(type == :rest || type == :keyrest)
    if type == :opt || type == :key
      param << Paint[name, :underline]
    else
      param << name.to_s
    end
    param << ":" if type == :key || type == :keyreq
    param
  }*", "

  puts template %(sig)
  nil
end
mof(obj, depth = nil, grep = //) click to toggle source
# File lib/debugging/mof.rb, line 6
def mof(obj, depth = nil, grep = //)
  grep = Regexp.new(grep)
  puts Paint["###", :red, :bold]

  if obj.is_a? Module
    klass, method_function = obj, :singleton_methods
    depth += 1 if depth
  else
    klass, method_function = obj.class, :public_instance_methods

    eigen_methods = obj.singleton_methods.grep(grep)
    if eigen_methods.empty?
      puts Paint['Eigenclass', :yellow]
    else
      puts Paint['Eigenclass', :green, :underline], eigen_methods.map(&:to_s)*'  '
    end
    puts
  end

  (depth || klass.ancestors.size).times{ |level|
    if cur = klass.ancestors[level]
      level_methods = cur.send(method_function, false).grep(grep)
      colors = level_methods.empty? ? [:yellow] : [:green, :underline]
      puts Paint["#{cur}", *colors], level_methods.map(&:to_s)*'  '
      puts unless level_methods.empty?
    end
  }

  nil
end
q(*args) click to toggle source
# File lib/debugging/q.rb, line 6
def q(*args)
  puts args.map{ |e| Paint[e.inspect, Paint.random] }*'  ' unless args.empty?
end
re(string, regex, groups = nil) click to toggle source
# File lib/debugging/re.rb, line 6
def re(string, regex, groups = nil)
  if regex =~ string
    if !groups
      puts $` + Paint[$&, :green] + $'
    else
      Array( groups ).each{ |group_nr|
        begin
          raise IndexError unless $~[group_nr]
          gr_string = string.dup
          gr_string[ $~.end( group_nr ),   0 ] = Paint::NOTHING
          gr_string[ $~.begin( group_nr ), 0 ] = Paint.color(:green)
          puts group_nr.to_s + ': ' + gr_string
        rescue IndexError
          puts group_nr.to_s + ': ' + Paint['no match', :red]
        end
      }
    end
  else
    puts Paint['no match', :red]
  end

  nil
end