If __FILE__ == $0 & ruby & rdebug & FXRuby

The following line acts differently under rdebug and ruby
  if __FILE__ == $0

FXRuby has the line, above, in nearly all its sample code.
  
Under rdebug, the value of $0 is, on my system,
  F:/InstantRails-2.0-win/ruby/bin/rdebug

Executing
  ruby -rdebug UltraDedup.rb
then the value of $0 is
  Debug.rb

Executing
  ruby UltraDedup.rb
then the value of $0 is
  UltraDededup.rb

How can I change things so that
  if __FILE__ == $0
acts correctly in each environment?

Ralph

The following line acts differently under rdebug and ruby
  if __FILE__ == $0

The name rdebug is ambiguous. Do you mean debug.rb, the debugger which
comes with ruby, or the ruby-debug gem? I suspect it's the latter,
since I have encountered this same bug with it.

FXRuby has the line, above, in nearly all its sample code.

Under rdebug, the value of $0 is, on my system,
  F:/InstantRails-2.0-win/ruby/bin/rdebug

Executing
  ruby -rdebug UltraDedup.rb
then the value of $0 is
  Debug.rb

Executing
  ruby UltraDedup.rb
then the value of $0 is
  UltraDededup.rb

This is an unfortunate bug in ruby-debug. In general, ruby-debug has
more problems for me than debug.rb, so I usually jsut use debug.rb.
The reason to prefer ruby-debug is because it's so much faster.
Sometimes, this becomes very important.

How can I change things so that
  if __FILE__ == $0
acts correctly in each environment?

I know of no really good solution. Typically, I manually set $0 to
__FILE__ at the beginning of a debugging session to work around this
problem (cursing all the while). I think this command is guaranteed to
do it:

  p $0=__FILE__

Tho with some versions of ruby-debug, I could leave off the p.

···

On 7/16/10, Ralph Shnelvar <ralphs@dos32.com> wrote:

$0 = The program you are running from the command line.

AKA you are running rdebug therefore $0 ==
F:/InstantRails-2.0-win/ruby/bin/rdebug

Enter irb and console:

<MANISH:jes> [07-16 09:01] 0 501:1 (14.43 Mb) • ~
! irb

irb(main):001:0> $0
=> "irb"

Make sense?

···

From: Ralph Shnelvar <ralphs@dos32.com>
Organization: Ralph Shnelvar
Reply-To: <ruby-talk@ruby-lang.org>
Date: Fri, 16 Jul 2010 21:27:36 +0900
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: if __FILE__ == $0 & ruby & rdebug & FXRuby

The following line acts differently under rdebug and ruby
  if __FILE__ == $0

FXRuby has the line, above, in nearly all its sample code.
  
Under rdebug, the value of $0 is, on my system,
  F:/InstantRails-2.0-win/ruby/bin/rdebug

Executing
  ruby -rdebug UltraDedup.rb
then the value of $0 is
  Debug.rb

Executing
  ruby UltraDedup.rb
then the value of $0 is
  UltraDededup.rb

How can I change things so that
  if __FILE__ == $0
acts correctly in each environment?

Ralph