How does File.read ("| echo mystring") work?

I found this in some code that I was working on. It was designed to work on the requirement to read a key file from disk. In production, the contents of the key file were in an environment variable.

Old code:

key = File.read('path/to/key.pem') 

New code:

 key = File.read('| echo $KEY_VARIABLE') 

How it works?

+5
source share
1 answer

From IO docs :

A line starting with "|" indicates a subprocess. The rest of the line following "|" It is called as a process with the corresponding input / output channels connected to it.

The “connected channels” bit means that the output of the process will become the input for read . Thus, in this example, you can read the result from the echo environment variable.

+9
source

Source: https://habr.com/ru/post/1261395/


All Articles