aug2uag
aug2uag

Reputation: 3445

$DEBUG global variable in Ruby

Can someone please summarize what/when is $DEBUG used. I am unfamiliar with it, and had no luck finding documentation (the Class Thread documentation and Module Signal documentation).

These are examples that are not clear to me what's going on:

p @ivar if $DEBUG

and:

Signal.trap("USR1") do
  $debug = !$debug
  puts "Debug now: #$debug"
end

Upvotes: 4

Views: 3447

Answers (1)

nneonneo
nneonneo

Reputation: 179422

Those two are totally different. The first is a debugging line executed only if $DEBUG is set; $DEBUG is set if you pass the command-line argument -d to ruby.

The second is flipping a $debug variable (different case!!), which is specific to the program. When you hit the process with a SIGUSR1 signal, it will change the debug mode (useful for getting it to print debugging information only sometimes).

Upvotes: 4

Related Questions