# [Quicky] Self contained Rails script

To run a script with your Rails application setup use `rails runner` [https://guides.rubyonrails.org/command\_line.html#bin-rails-runner](https://guides.rubyonrails.org/command_line.html#bin-rails-runner)

```scala
# 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`):

```ruby
#!/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:

```plaintext
chmod +x bin/puts_env
```

Now you can run the script, no more need to remember how to run it, just do:

```plaintext
./bin/puts_env
```

## Magical!
