Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

script runner should overwrite $0

#2244

When loading Ruby code from a file, script/runner should rename $0 (and $PROGRAM_NAME) to that file name.

Reported by Kiyoshi · March 16th, 2009 @ 12:15 AM

State: resolved
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Steve St. Martin
    Steve St. Martin
    • Assigned user set to Ryan Bigg

    please submit a patch. mark as incomplete

    April 15th, 2010 @ 09:39 PM

  2. Ryan Bigg
    Ryan Bigg
    • State changed from new to incomplete

    April 15th, 2010 @ 10:37 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Rohit Arondekar
    Rohit Arondekar
    • State changed from incomplete to open

    I've attached a patch that does what Kiyoshi has asked for.

    June 19th, 2010 @ 09:17 AM

  5. Rohit Arondekar
    Rohit Arondekar
    • Tag changed from runner, script to patch, runner, script

    Ignore previous patch. This one properly sets $0 and $PROGRAM_NAME to the filename.

    June 19th, 2010 @ 11:34 AM

  6. David Trasbo
    David Trasbo

    +1

    I tried it with a fresh Rails 3 app and it works.

    I think the variables should be set like app/models/person.rb instead of just person.rb (relatively to Rails.root) to be consistent with normal $0 and $PROGRAM_NAME behavior.
    If you, for example, run ruby foo/bar/baz.rb the two variables will both be set to foo/bar/baz.rb.

    June 20th, 2010 @ 03:12 PM

  7. Rohit Arondekar
    Rohit Arondekar
    • Assigned user changed from Ryan Bigg to José Valim

    Thanks David. Actually the first patch behaves like that. It will set $0 and $PROGRAM_NAME to the argument passed to runner, in case the file exists.

    June 20th, 2010 @ 03:21 PM

  8. Rohit Arondekar
    Rohit Arondekar

    I couldn't find any tests for Rails Runner. Does it need tests? I wouldn't mind having a go at writing some.

    June 21st, 2010 @ 02:08 AM

  9. José Valim
    José Valim

    Hey Rohit, please do add a few tests (at least one for filename and another one for simply passing a string). You can take as base this rake test in Railties:

    http://github.com/rails/rails/blob/master/railties/test/application...

    June 21st, 2010 @ 10:08 AM

  10. Rohit Arondekar
    Rohit Arondekar

    I just started on this, but another ticket #4249 [rails3 beta] "rails runner" can't be used in shebang lines has opened up which may change a few things. So I think it's best if I wait?

    June 22nd, 2010 @ 06:01 AM

  11. Rohit Arondekar
    Rohit Arondekar

    Attached patch that resolves the issue and also adds 4 tests for runner.

    June 22nd, 2010 @ 02:18 PM

  12. Repository
  13. Repository
    Repository
    • State changed from open to resolved

    (from [40bf76165c392a15cd74321dd7d1cbc96c77bd27]) Rails Runner now sets $0 and $PROGRAM_NAME to name of file being run [#2244 script runner should overwrite $0 state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/40bf76165c392a15cd74321dd7d1cb...

    June 22nd, 2010 @ 02:22 PM