$0 == false?

I’m running a script under mod_ruby, and for some reason $0 is set to false.
Two questions, then:

Why would it be false, instead of ‘myscript.rb’? Has mod_ruby clobbered it?

Without $0, how can I retrieve the name of the currently running script from
within a require’d file? (ie, FILE won’t work…) Example:

tim@zaphod:~$ cat a.rb
require ‘b.rb’

tim@zaphod:~$ cat b.rb
puts "$0 == #{$0}"
puts “FILE == #{FILE}”

tim@zaphod:~$ ruby a.rb
$0 == a.rb
FILE == ./b.rb
tim@zaphod:~$

In b.rb, I want to retrieve the value ‘a.rb’, so FILE is no good, but if
$0 has been clobbered, what can I do?

Tim Bates

···


tim@bates.id.au

Okay, it seems I can use Apache.request.filename instead, but I’m still
curious about the clobbering of $0.

Tim Bates

···

On Sun, 9 Mar 2003 6:29 pm, Tim Bates wrote:

Without $0, how can I retrieve the name of the currently running script
from within a require’d file? (ie, FILE won’t work…) Example:


tim@bates.id.au

Tim Bates tim@bates.id.au wrote in message news:200303092049.04170.tim@bates.id.au

Without $0, how can I retrieve the name of the currently running script
from within a require’d file? (ie, FILE won’t work…) Example:

Okay, it seems I can use Apache.request.filename instead, but I’m still
curious about the clobbering of $0.

You can also use ‘caller’ in the require’d file:

b.rb

puts caller[0][/[^:]+/]

As for why $0 gets clobbered, someone with any mod_ruby experience
will have a better answer, but here are some guesses:

  1. All scripts in mod_ruby are run under the same process
    as the Apache server?
  2. All scripts share the same global variables?
  3. Instead of returning $0 == ‘foo.rb’ for all scripts,
    mod_ruby punts?

Again, all guesses. Would it be possible for mod_ruby to set $0 to
Apache.request.filename just before running a script?

BTW, how does mod_ruby set $0 to false?

$0 = nil # …failed to convert nil into String (TypeError)

And, an off-topic challenge: anyone know how to get $0 to return the
current time?

puts $0 # Mon Mar 10 02:21:26 PST 2003
sleep 10
puts $0 # Mon Mar 10 02:21:36 PST 2003

···

On Sun, 9 Mar 2003 6:29 pm, Tim Bates wrote:


Tabby

uhhh… why would you want that? That’s totally silly

use Time.now

···

Sabby and Tabby (sabbyxtabby@yahoo.com) wrote:

And, an off-topic challenge: anyone know how to get $0 to return the
current time?

puts $0 # Mon Mar 10 02:21:26 PST 2003
sleep 10
puts $0 # Mon Mar 10 02:21:36 PST 2003


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Why would anyone want to climb Mt. Everest? It’s a challenge! :slight_smile:

Yes, returning the current time is silly. Perhaps returning
Apache.Request.filename is less silly. The idea is to have $0
invoke a method to return a calculated value, something akin to
Perl’s tie() mechanism.

A quick search turns up StringMixin in MetaRuby. Unfortunately,
#puts only calls #to_s on non-String objects. And $0 can only be
assigned a String/subclass – is $0 an example of a typed variable
in Ruby?

Admittedly, the challenge is probably impossible for $0, but doable
for normal variables.

···

Eric Hodel drbrain@segment7.net wrote:

Sabby and Tabby (sabbyxtabby@yahoo.com) wrote:

And, an off-topic challenge: anyone know how to get $0 to return the
current time?

puts $0 # Mon Mar 10 02:21:26 PST 2003
sleep 10
puts $0 # Mon Mar 10 02:21:36 PST 2003

uhhh… why would you want that? That’s totally silly

use Time.now


Tabby