Module: Kernel

Defined in:
lib/puts_debug.rb

Instance Method Summary collapse

Instance Method Details

#pd(h = {}, line_symbol = nil, show_lines = :both, empty_lines_margin = nil) ⇒ Object

For easier puts debugging (pd). Defined in Kernel module which is included into Object class so this method is accessible from everywhere. Output is sent to standard error stream (stderr) so it could be further separated from standard output like this:

rails s 2>> error

then you can

tail -f error

to watch error stream output.

Examples:

  pd('something') =>
  something

  pd(name: 'Yogurt') =>
  name: Yogurt

  pd(1, '-', :both, 1) =>

  ------------------------------------------------------------
  1
  ------------------------------------------------------------

  pd({a: 1, b: 3}, '*', :both, 2) =>

  ************************************************************
  a: 1
  b: 3
  ************************************************************


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

def pd(h = {}, line_symbol = nil, show_lines = :both, empty_lines_margin = nil)

  return unless Rails.env.development?

  $stderr.puts "\n" * empty_lines_margin if empty_lines_margin.present? && show_lines.in?([:both, :top])
  $stderr.puts (line_symbol * 60) if line_symbol.present? && show_lines.in?([:both, :top])

  if h.is_a? Hash
    h.each do |k, v|
      $stderr.puts "#{k}: #{v}"
    end
  else
    $stderr.puts h
  end

  $stderr.puts line_symbol * 60 if line_symbol.present? && show_lines.in?([:both, :bottom])
  $stderr.puts "\n" * empty_lines_margin if empty_lines_margin.present? && show_lines.in?([:both, :bottom])

end