mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Make An Executable Ruby Script as a Ruby TIL
This commit is contained in:
38
ruby/make-an-executable-ruby-script.md
Normal file
38
ruby/make-an-executable-ruby-script.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Make An Executable Ruby Script
|
||||
|
||||
In a unix environment with Ruby available, I can make a Ruby script. To do this
|
||||
I stick some code in a Ruby file, like `database_url.rb`.
|
||||
|
||||
```ruby
|
||||
result = `heroku pg:credentials:url DATABASE_URL --app my-app`
|
||||
puts result.split("\n")[2].strip
|
||||
```
|
||||
|
||||
And then execute that file with `ruby`:
|
||||
|
||||
```bash
|
||||
$ ruby database_url.rb
|
||||
```
|
||||
|
||||
I can instead make an executable file that doesn't need to be explicitly
|
||||
invoked with the `ruby` command. To do this, I need to prefix my file with a
|
||||
[shebang](https://unix.stackexchange.com/a/87600/5916) for
|
||||
[`ruby`](https://devcenter.heroku.com/articles/ruby-binstub-shebang).
|
||||
|
||||
And I'll even just call the file `database_url` now, no file suffix.
|
||||
|
||||
```ruby
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
result = `heroku pg:credentials:url DATABASE_URL --app my-app`
|
||||
puts result.split("\n")[2].strip
|
||||
```
|
||||
|
||||
When executed, this script will see the first line and understand that it needs
|
||||
to execute the rest of the script using `ruby` as the interpreter.
|
||||
|
||||
Like any other executable, you can call it as is, like so:
|
||||
|
||||
```ruby
|
||||
$ database_url
|
||||
```
|
||||
Reference in New Issue
Block a user