Module: Kernel
- Defined in:
- lib/puts_debug.rb
Instance Method Summary collapse
-
#pd(args) ⇒ Object
For easier puts debugging (pd).
Instance Method Details
#pd(args) ⇒ 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(data: 'something', data_color: 'magenta', lines: :none) =>
something
pd(name: 'Yogurt') =>
============================================================
name: Yogurt
============================================================
pd(data: 1, symbol: '-', margin: 1) =>
------------------------------------------------------------
1
------------------------------------------------------------
pd(data: {a: 1, b: 3}, symbol: '*') =>
************************************************************
a: 1
b: 3
************************************************************
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/puts_debug.rb', line 43 def pd(args) params = nil if args.is_a? Hash if args[:data].nil? params = { data: args } else params = args end else params = { data: args } end # parameters data = params[:data] symbol = params[:symbol] || '=' line_color = params[:color] || 'red' data_color = params[:data_color] || nil margin = params[:margin] || 0 lines = params[:lines] || :both title = params[:title] epilogue = params[:epilogue] # locals line_str = Rainbow(symbol * 60).try(line_color) margin_str = "\n" * margin $stderr.puts margin_str if margin > 0 && lines.in?([:both, :top]) $stderr.puts line_str if lines.in?([:both, :top]) if title.present? title_str = if title.ends_with? ':' "#{title}\n" else "#{title}:\n" end $stderr.puts Rainbow(title_str).try(line_color) end data_str = case data when Hash data.map { |k, v| "#{k}: #{v}" }.join("\n") when Array data.map { |v| "#{v}" }.join(",\n") else data.to_s end data_str = Rainbow(data_str).try(data_color) if data_color.present? $stderr.puts data_str if title.present? $stderr.puts '' end $stderr.puts Rainbow(epilogue).try(line_color) if epilogue.present? if lines.present? $stderr.puts line_str if lines.in?([:both, :bottom]) $stderr.puts margin_str if margin > 0 && lines.in?([:both, :bottom]) end end |