Argv

Hi,

how do I recognize if a ruby file prg.rb was called as a separate program
(from the command line) or was just included using require. Testing ARGV
for being nil doesn’t seem to work

···

######################################################################

file= prg.rb

######################################################################

if ARGV
print "called from command line"
end

######################################################################

file= prg2.rb

######################################################################
require ‘prg.rb’

print “now in prg2”

######################################################################

calling

%ruby1.6 prg2.rb

produces

called from command line
now in prg2

and NOT just
now in prg2

So what’s wrong, I thought it would be the “way of least suprise” to test
ARGV for being nil in the above situation.

Any suggestions?

Artur


Artur
>>>
(O-O)
_________________________.oo0–()–0oo.

if $0 == FILE

I was called from the command line

else

I was 'require’d

end

Ian

···

On Wed 04 Feb 2004 at 20:05:04 +0900, Artur Merke wrote:

how do I recognize if a ruby file prg.rb was called as a separate program
(from the command line) or was just included using require. Testing ARGV
for being nil doesn’t seem to work


Ian Macdonald | Anger kills as surely as the other vices.
System Administrator |
ian@caliban.org |
http://www.caliban.org |
>

Hi –

Hi,

how do I recognize if a ruby file prg.rb was called as a separate program
(from the command line) or was just included using require. Testing ARGV
for being nil doesn’t seem to work

See Ian’s answer – but also, just for future reference if needed,
note that ARGV is never nil, but is an array (possibly an empty one).

######################################################################

file= prg.rb

######################################################################

if ARGV
print “called from command line”
end

######################################################################

file= prg2.rb

######################################################################
require ‘prg.rb’

print “now in prg2”

Note also that the emptiness of ARGV will depend on how prg2.rb is
called. So if you do:

ruby prg2.rb abc

ARGV will not be empty.

David

···

On Wed, 4 Feb 2004, Artur Merke wrote:


David A. Black
dblack@wobblini.net