# Getting rid of $0

**URL:** <https://www.ruby-forum.com/t/getting-rid-of-0/108363>\
**Category:** Ruby\
**Created:** [July 2, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363 "2007-07-02T19:59:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 2, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/1 "2007-07-02T19:59:42Z")

</div>

The common idiom  
if **FILE** == $0 then …  
gives bugs when $0 is not what one would expect, for instance under a  
debugger, or in rubyscript2exe, etc.

There should be a primitive test  
if then…

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 2, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/2 "2007-07-02T20:10:49Z")

</div>

On 7/2/07, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The common idiom  
> if **FILE** == $0 then …  
> gives bugs when $0 is not what one would expect, for instance under a  
> debugger, or in rubyscript2exe, etc.
> 
> There should be a primitive test  
> if then…

require ‘English’

if **FILE** == $PROGRAM\_NAME  
…  
end

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 2, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/3 "2007-07-02T20:12:50Z")

</div>

Tim P. wrote:

> require ‘English’
> 
> if **FILE** == $PROGRAM\_NAME  
> …  
> end

Does that solve the problem? Isn’t that vulnerable to the same issues  
as just using $0?

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 2, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/4 "2007-07-02T20:22:51Z")

</div>

On 7/2/07, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > require ‘English’
> > 
> > if **FILE** == $PROGRAM\_NAME  
> > …  
> > end
> 
> Does that solve the problem? Isn’t that vulnerable to the same issues  
> as just using $0?

You’re correct! That does not solve the problem. 😕

Hmmm … the solution I use most often is to have a top-level function  
that runs the application. This top-level function is called by a  
separate Ruby script.

class MyApp  
def self.run( args )  
new(args).run  
end  
…  
end

And in an executable file called “my\_app”

#!/usr/bin/env ruby

require ‘MyApp’  
MyApp.run ARGV

I try to avoid the **FILE** == $0 idiom when I distribute code. You  
run into the problems the original poster pointed out, and rubygems  
also wrappers up all your executables with it’s own special calling  
semantics (provides compatibility between windows / \*nix).

Not the answer you want to hear, but, “don’t do that”. Sorry 😕

TwP

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 2, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/5 "2007-07-02T23:04:16Z")

</div>

On 7/2/07, Michel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The common idiom  
> if **FILE** == $0 then …  
> gives bugs when $0 is not what one would expect, for instance under a  
> debugger, or in rubyscript2exe, etc.
> 
> There should be a primitive test  
> if then…

rcov provides for this case a special switch --replace-progname that  
does what the name says. Unfortunately, sometimes there’s still a  
mismatch between those two:  
one is ./something.rb while the other is just something.rb. Therefore  
I use the following variation of the idiom (I haven’t checked this  
under debugger or any other prog):

if File.expand\_path( **FILE** )==File.expand\_path($0)

I have posted a patch for ruby-prof that adds a similar switch and it  
has been accepted.

Anyway, $0 seems to be a normal variable that you can set to anything  
you want, so creating a small wrapper script should not be a problem.  
E.g.

$0 = ARGV.shift  
require $0

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 2, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/6 "2007-07-02T22:48:13Z")

</div>

On Jul 2, 2007, at 11:11 , Alex Y. wrote:

> issues as just using $0?  
> Solve what problem? The code is doing what you asked it to do, that  
> isn’t a problem, that’s programming.

The code clearly reads “if this file is the program” which is NOT the  
case when run under a debugger. I’m not sure what rubyscript2exe  
generates, but my guess is that can be solved on their end somehow. I  
generally don’t want that code to run when I’m poking around in a  
debugger or something similar. If I do, I set the values inside the  
debugger and don’t worry about it.

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [July 3, 2007, 8:44am UTC](https://www.ruby-forum.com/t/getting-rid-of-0/108363/7 "2007-07-03T08:44:17Z")

</div>

On Jul 2, 2007, at 3:03 PM, Jano S. wrote:

> Anyway, $0 seems to be a normal variable that you can set to anything  
> you want, so creating a small wrapper script should not be a problem.  
> E.g.
> 
> $0 = ARGV.shift  
> require $0

be careful, $0 is not a normal variable:

cfp:~ \> cat a.rb  
$0 = ‘foobar’

puts `ps wwwaux|grep foobar`

cfp:~ \> ruby a.rb  
ahoward 10371 2.6 -0.1 29204 1388 p2 S+ 12:41AM 0:00.01  
foobar  
ahoward 10372 1.4 -0.0 27728 584 p2 S+ 12:41AM 0:00.00  
sh -c ps wwwaux|grep foobar  
ahoward 10374 1.2 -0.0 27368 448 p2 S+ 12:41AM 0:00.00  
grep foobar

it actually assigns to c’s argv

-a
