From: Tim Pease Date: 2007-07-03T03:21:44+09:00 Subject: Re: getting rid of $0 On 7/2/07, Alex Young wrote: > Tim Pease wrote: > > On 7/2/07, Michel Demazure 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 > > > 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