ARGV

A

Artur Merke

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.___________________
 
I

Ian Macdonald

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

if $0 == __FILE__
# I was called from the command line
else
# I was 'require'd
end

Ian
--
Ian Macdonald | Anger kills as surely as the other vices.
System Administrator |
(e-mail address removed) |
http://www.caliban.org |
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top