Module: DebugMe

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

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Instance Method Details

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



5
6
7
8
9
10
11
12
13
14
15
16
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
# File 'lib/debug_me.rb', line 5

def debug_me(options = {}, &block)
  default_options = {
    tag: 'DEBUG:',  # A tag to prepend to each output line
    time: true,      # Include a time-stamp in front of the tag
    header: true,      # Print a header string before printing the variables
    ivar: true,      # Include instance variables in the output
    cvar: true,      # Include class variables in the output
    file: $stdout    # The output file
  }

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

  f = options[:file]
  s = ''
  s += "#{sprintf('%010.6f', Time.now.to_f)} " 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

  f.puts "#{s} Source: #{wf}" if options[:header]

  if block_given?

    block_value = [block.call].flatten.compact

    if block_value.empty?
      block_value = eval('local_variables', block.binding)
      block_value += [eval('instance_variables', block.binding)] if options[:ivar]
      block_value += [self.class.send('class_variables')] if options[:cvar]
      block_value = block_value.flatten.compact
    else
      block_value.map!(&:to_s)
    end

    block_value.each do |v|
      ev = eval(v, block.binding)
      f.puts "#{s} #{v} -=> #{ev.pretty_inspect}"
    end

  end ## if block_given?

  f.flush
end