[Quicky] Self contained Rails script
![[Quicky] Self contained Rails script](https://cdn.hashnode.com/res/hashnode/image/upload/v1710318775142/e0d2022e-d354-4a20-beaa-be398dda5cc6.png)
10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
To run a script with your Rails application setup use rails runner https://guides.rubyonrails.org/command_line.html#bin-rails-runner
# inline script
./bin/rails runner "puts Rails.env"
# file
./bin/rails runner path/to/file.rb
If you want to have a self container, here's what you need (eg: filename bin/puts_env):
#!/usr/bin/env ruby
APP_PATH = File.expand_path("../../config/application", __FILE__)
require File.expand_path("../../config/boot", __FILE__)
require APP_PATH
Rails.application.require_environment!
###### Your script below
puts Rails.env
Don't forget to make the file executable:
chmod +x bin/puts_env
Now you can run the script, no more need to remember how to run it, just do:
./bin/puts_env
