mirror of
https://github.com/jbranchaud/til
synced 2026-01-11 11:08:02 +00:00
Add Get The Output Of Running A System Program as a Ruby TIL
This commit is contained in:
25
ruby/get-the-output-of-running-a-system-program.md
Normal file
25
ruby/get-the-output-of-running-a-system-program.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# Get The Output Of Running A System Program
|
||||
|
||||
Ruby has a [variety of ways](https://stackoverflow.com/a/18623297/535590) to
|
||||
execute a system program within a Ruby process. The backticks approach is the
|
||||
handy shorthand approach to reach for if you want to capture the output of the
|
||||
command.
|
||||
|
||||
For instance, let's say I want to capture the connection string credentials
|
||||
output by running a `heroku` command. When the command is wrapped in backticks,
|
||||
Ruby will execute it in a subprocess and the output of the command will be the
|
||||
return value.
|
||||
|
||||
```ruby
|
||||
result = `heroku pg:credentials:url DATABASE_URL --app my-app`
|
||||
|
||||
# extract connection details
|
||||
connection_info = result.split("\n")[2].strip
|
||||
```
|
||||
|
||||
With the result in hand, I can use Ruby to parse out the details I'm interested
|
||||
in.
|
||||
|
||||
Backticks does two other nice things. It allows for string interpolation and it
|
||||
puts the process status (e.g. exit code) on
|
||||
[`$?`](check-return-status-of-running-a-shell-command.md).
|
||||
Reference in New Issue
Block a user