Ruby dbg!

Because I wrote:

p '!!!!!!!!!!!!!!!'
p msg
p '!!!!!!!!!!!!!!!'

too many times already.

dbg! is a minimal, Rust inspired, puts debugging command for Ruby. It provides caller context and formatting helpful in everyday debugging tasks.

Installation

Gemfile

gem "ruby-dbg"

It adds a global dbg! method. You can use it for puts debugging:

require "ruby-dbg"

dbg!(User.last.id)
# [web/user_sessions_controller.rb:37] 1972

It appends relevant caller file and line info to the debug output.

You can use symbols to output local variable names together with their values:

a = 1
b = 2 

dbg!(:a, :b)
# [models/user.rb:22] a = 1
# [models/user.rb:22] b = 2

Hash values are pretty printed:


dbg!(User.last.as_json)
# [web/users_controller.rb:10 {
#   "id": 160111,
#   "team_id": 1,
#   "pseudonym": "Anonymous-CBWE",
#   ...
# }

You can enable coloring the output:

config/initializers/ruby_dbg.rb

require "ruby-dbg"

RubyDBG.color_code = 36 # light blue
# 31 red 
# 32 green 
# 33 yellow 
# 34 blue 
# 35 pink 

Diagnose report

If it does not stand out enough, you can enable dbg! highlighting:

require "ruby-dbg"

RubyDBG.highlight!("πŸŽ‰πŸ’”πŸ’£πŸ•ΊπŸš€πŸ§¨πŸ™ˆπŸ€―πŸ₯³πŸŒˆπŸ¦„")

Diagnose report

Status

Contributions & ideas very welcome!