10

I use RubyMine to write and debug my Ruby 2.0 code. It uses ruby-debug-ide for that purpose. I want to know if a program is running in debug mode.

I know there is the Ruby $DEBUG global variable, but as far as I understand ruby-debug-ide didn't change it, because it didn't use the -d ruby flag.

If I debug my file using Rubymine the command executed looks like this:

/home/user/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -e at_exit{sleep(1)};$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/user/.rvm/gems/ruby-2.0.0-p353/gems/ruby-debug-ide-0.4.22/bin/rdebug-ide --disable-int-handler --port 37737 --dispatcher-port 47992 -- /home/user/file.rb

I tried to use ARGV or $0, to determine if the command line contains the string 'rdebug-ide' but ARGV is an empty array and $0 is just '/home/user/file.rb', how can I get the full command line executed by RubyMine?

toro2k
  • 19,020
  • 7
  • 64
  • 71
ShockwaveNN
  • 2,227
  • 2
  • 29
  • 56

2 Answers2

7

This is what I did:

I put the following code in an (rails) action and did a diff on the outputs both in debug and non-debug modes:

puts ENV.to_hash.to_yaml

I noticed that one of the differences is in ENV['RUBYLIB'] (there's also IDE_PROCESS_DISPATCHER, DEBUGGER_STORED_RUBYLIB, RUBYOPT, and DEBUGGER_HOST)

So here's how you'd check:

if ENV['RUBYLIB'] =~ /ruby-debug-ide/
  puts 'in debug mode'
else
  puts 'not in debug mode'
end
Abdo
  • 13,549
  • 10
  • 79
  • 98
  • 1
    Works, at least in RubyMine + rvm. Thanks! – skywinder Mar 03 '15 at 09:42
  • @PerLundberg check my method and do your own diff to figure out what works for you environment – Abdo Aug 09 '17 at 09:34
  • @Abdo Checked now. None of RUBYLIB or the other env variables mentioned have any unusual values when being run through the VSCode debugger. – Per Lundberg Aug 10 '17 at 10:57
  • @PerLundberg I don't use VSCode but I suggest changing the debugging command (there should be somewhere where you can add variables to it). Now that I'm thinking about this question/answer, I'm not sure that was useful. Have you tried `gem pry-rails` ? – Abdo Aug 10 '17 at 16:29
  • As @PerLundberg mentioned, on VSCode running `puts ENV.to_hash.to_yaml` with (F5) or without (Ctrl+F5) debugger returns the same output. – Halil Sen Mar 05 '19 at 08:50
1

You need the global variable $LOAD_PATH.

a = $LOAD_PATH
a.each do |current_path|
    puts 'Debug mode' if current_path.include?('rb/gems')
end

$LOAD_PATH has this line "/home/username/RubyMine-6.0.2/rb/gems" if I use debug mode.

toro2k
  • 19,020
  • 7
  • 64
  • 71
Flamine
  • 487
  • 4
  • 21
  • Welcome to Stack Overflow! Can you please edit your answer to include an example showing how to use the variable `$LOAD_PATH` to achieve what OP wants? – toro2k Feb 26 '14 at 12:10
  • Sorry for mislead. For me this variant work only if clean .rb file. But if I use it in my project - both debug and run $LOAD_PATH contains 'ruby-debug', don't know why – ShockwaveNN Feb 26 '14 at 12:19