Module: DebugMe

Defined in:
lib/debug_me.rb,
lib/debug_me/version.rb

Constant Summary collapse

VERSION =
'1.0.4'

Instance Method Summary collapse

Instance Method Details

#debug_me(options = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/debug_me.rb', line 17

def debug_me(options = {}, &block)

  if 'Hash' == options.class.to_s
    options = DebugMeDefaultOptions.merge(options)
  else
    options = DebugMeDefaultOptions.merge(tag: options)
  end

  out_string = ''

  f = options[:file]
  s = ''
  s += Time.now.strftime(options[:strftime])+' ' if options[:time]
  s += "#{options[:tag]}"
  wf = caller # where_from under 1.8.6 its a stack trace array under 1.8.7 is a string
  wf = wf[0] if 'Array' == wf.class.to_s

  out_string = sprintf("%s Source: %s\n",s,wf) if options[:header]

  if block_given?

    block_value = [block.call].flatten.compact

    if block_value.empty?
      block_value = []
      block_value += [eval('local_variables', block.binding)] if options[:lvar]
      block_value += [eval('instance_variables', block.binding)] if options[:ivar]
      block_value += [self.class.send('class_variables')] if options[:cvar]
      block_value += [self.class.constants] if options[:cconst]
      block_value = block_value.flatten.compact
    end

    block_value.map!(&:to_s)

    block_value.each do |v|
      ev = eval("defined?(#{v})",block.binding).nil? ? '<undefined>' : eval(v, block.binding)
      out_string += sprintf( "%s %s -=> %s", s,v,ev.pretty_inspect)
    end

  end ## if block_given?

  unless f.nil?
    f.puts out_string
    f.flush
  end

  return out_string
end