Of the approaches mentioned earlier, none of them looked clean and perfect, as you would expect a stand-alone script to run (not get an estimate or pass through <redirection), but in the end, this works great for me:
(for Rails 3)
Paste at the top of your script:
#!/usr/bin/env ruby APP_PATH = File.expand_path(appdir = '/srv/staging/strat/fundmgr/config/application', __FILE__) require File.expand_path(appdir + '/../boot', __FILE__) require APP_PATH
Of course, configure your own Rails path in the APP_PATH line.
That way I can avoid typing any interactive irb or rails c and can check my script.rb from the command line before, for example. planning it in crontab.
It smoothly supports command line options and minimizes wrapper levels before moving on to your code.
CREDIT (also shows an example of Rails 2)
http://zerowidth.com/2011/03/18/standalone-script-runner-bin-scripts-in-rails.html
Marcos Oct 27 '14 at 15:56 2014-10-27 15:56
source share