API Reference¶
Module: DebugMe¶
The main module providing debugging functionality.
Including the Module¶
After including, the debug_me method becomes available in the current scope.
Method: debug_me¶
The primary debugging method. Outputs labeled variable values to the configured destination.
Parameters¶
| Parameter | Type | Description |
|---|---|---|
tag |
String | (Optional) Shorthand for tag: option |
options |
Hash | Configuration options (see below). After a tag, pass them as keywords, or splat a Hash with ** |
&block |
Block | Block yielding variable names to inspect |
Shorthand Syntax¶
When the first argument is a string, it's used as the tag option:
# These calls are equivalent:
debug_me(tag: 'INFO') { :status }
debug_me('INFO') { :status }
# Shorthand with additional options:
debug_me('TRACE', levels: 2, time: false) { :data }
# Same as:
debug_me(tag: 'TRACE', levels: 2, time: false) { :data }
Options Hash¶
| Key | Type | Default | Description |
|---|---|---|---|
tag |
String | 'DEBUG' |
Label prepended to output |
time |
Boolean | true |
Include timestamp |
strftime |
String | '%Y-%m-%d %H:%M:%S.%6N' |
Timestamp format |
header |
Boolean | true |
Show source file and line number |
levels |
Integer | 0 |
Number of backtrace levels to include |
skip1 |
Boolean | false |
Add one blank line before output |
skip2 |
Boolean | false |
Add two blank lines before output |
lvar |
Boolean | true |
Include local variables (empty block) |
ivar |
Boolean | true |
Include instance variables (empty block) |
cvar |
Boolean | true |
Include class variables (empty block) |
cconst |
Boolean | true |
Include constants (empty block) |
logger |
Logger | nil |
Logger instance for output |
file |
IO | $stdout |
Output destination |
Block Syntax¶
The block should yield one of:
| Yield Type | Example | Description |
|---|---|---|
| Symbol | { :variable } |
Single variable name |
| Array of Symbols | { [:var1, :var2] } |
Multiple variable names |
| String | { 'expression' } |
Expression to evaluate |
| Array of Strings | { ['expr1', 'expr2'] } |
Multiple expressions |
| Empty | {} |
All variables in scope |
| Any other value | { obj } |
Printed as it is, labeled by its class name (a Class or Module by its own name) |
Arrays are flattened and nil values are skipped. Arrays may mix value types: debug_me { [:name, Pathname.pwd] } prints name -=> "Alice" and Pathname -=> #<Pathname:...>.
A Symbol or String that names nothing defined in the caller's scope prints the String "<undefined>" as its value and does not raise: debug_me { :nope } prints DEBUG nope -=> "<undefined>". A typo in a name therefore fails quietly.
Return Value¶
Returns the output string if $DEBUG_ME is true, otherwise returns nil.
Examples¶
# Single variable
name = "Alice"
debug_me { :name }
# Multiple variables
x, y = 10, 20
debug_me { [:x, :y] }
# Instance variables
debug_me { [:@user, :@session] }
# Class variables
debug_me { [:@@counter] }
# Expressions
debug_me { 'array.size' }
debug_me { ['hash.keys', 'hash.values'] }
# All local variables
debug_me {}
# With options
debug_me(tag: 'INFO', levels: 2) { :data }
Global Variable: $DEBUG_ME¶
Controls whether debug output is enabled globally.
Values¶
| Value | Effect |
|---|---|
true |
Debug output is enabled (default) |
false |
All debug output is suppressed |
Behavior When Disabled¶
When $DEBUG_ME is false:
debug_mereturns immediately- The block is not evaluated
- No output is produced
- Zero performance overhead
Example¶
$DEBUG_ME = true
debug_me { :value } # Outputs debug information
$DEBUG_ME = false
debug_me { :value } # No output, block not evaluated
Environment Variable: DEBUG_ME¶
Controls the initial value of $DEBUG_ME at load time.
Supported Values¶
| Enable Values | Disable Values |
|---|---|
true |
false |
yes |
no |
on |
off |
1 |
0 |
Usage¶
# Enable debug output
DEBUG_ME=true ruby script.rb
# Disable debug output
DEBUG_ME=false ruby script.rb
Constant: DebugMeDefaultOptions¶
The default options hash used when options are not specified.
Default Values¶
DebugMeDefaultOptions = {
tag: 'DEBUG',
time: true,
strftime: '%Y-%m-%d %H:%M:%S.%6N',
header: true,
levels: 0,
skip1: false,
skip2: false,
lvar: true,
ivar: true,
cvar: true,
cconst: true,
logger: nil,
file: $stdout
}
Modifying Defaults¶
# Change default tag
DebugMeDefaultOptions[:tag] = 'TRACE'
# Disable timestamps by default
DebugMeDefaultOptions[:time] = false
Constant: DEBUG_ME_MAX_BACKTRACE¶
Maximum number of backtrace entries printed when the block returns :backtrace and no levels backtrace was collected (levels: 0 or header: false). It does not cap the levels option. Value: 10000
Output Format¶
Each line of debug_me output has the form:
The timestamp is an unbracketed strftime('%Y-%m-%d %H:%M:%S.%6N') value (omitted
when time: false), TAG defaults to DEBUG, and the Source: header line is
omitted when header: false.
Standard Output¶
2026-11-09 12:30:45.123456 DEBUG Source: example.rb:4:in '<main>'
2026-11-09 12:30:45.123456 DEBUG count -=> 5
With Backtrace¶
A backtrace prints as a single backtrace -=> entry. Its value depends on how
it was requested: levels: gives an array of [index, "frame"] pairs, while
returning :backtrace with no levels: gives an array of plain frame strings.
require 'debug_me'
include DebugMe
def outer
value = "x"
debug_me(levels: 2) { :value }
debug_me { [:backtrace, :value] }
end
def middle = outer
middle
2026-11-12 02:26:47.300264 DEBUG Source: example.rb:6:in 'Object#outer'
2026-11-12 02:26:47.300264 DEBUG backtrace -=> [[0, "example.rb:10:in 'Object#middle'"], [1, "example.rb:11:in '<main>'"]]
2026-11-12 02:26:47.300264 DEBUG value -=> "x"
2026-11-12 02:26:47.300782 DEBUG Source: example.rb:7:in 'Object#outer'
2026-11-12 02:26:47.300782 DEBUG backtrace -=> ["example.rb:10:in 'Object#middle'", "example.rb:11:in '<main>'"]
2026-11-12 02:26:47.300782 DEBUG value -=> "x"
Multiple Variables¶
2026-11-09 12:30:45.123456 DEBUG Source: example.rb:4:in '<main>'
2026-11-09 12:30:45.123456 DEBUG var1 -=> 1
2026-11-09 12:30:45.123456 DEBUG var2 -=> 2
2026-11-09 12:30:45.123456 DEBUG var3 -=> 3