Well, you know what ./script/runner is?
If so, you’ll know you can’t use it to build script without using an absolute path in the shebang line in your custom scripts:
> ./script/runner -h
Usage: ./script/runner [options] ('Some.ruby(code)' or a filename)
You can also use runner as a shebang line for your scripts like this:
#!/usr/bin/env /path/to/your/app/script/runner
...
So here’s the solution! (here the script is saved in the script dir)
#!/usr/bin/env ruby
unless $0 =~ /runner/
exec("#{File.dirname(__FILE__)}/runner #{__FILE__}")
# "exec" exits the current processing, so this comment won't reached (thanks to Arthaey Angosii)
end
# Your great script goes here!
Alternative:
#!/usr/bin/env ruby $: << File.dirname(__FILE__) ARGV[0] = __FILE__ load 'runner' #your code here


If you use exec() instead of backticks, STDOUT will continue to work. Otherwise, the backticks will swallow any writes to STDOUT by the rest of the script.
You’re right, thanks for the suggestion! :)
your alternative solution runs code twice ;)
This one works, uses default environment. If you want to override the environment, set ENV(’RAILS_ENV’)
——–
#!/usr/bin/env ruby
require File.dirname(__FILE__) + ‘/../config/boot’
require RAILS_ROOT + ‘/config/environment’
puts “Running in environment #{RAILS_ENV}…”
(your code goes here)