我如何 运行 带有 ENV 变量的本地非网络 Ruby 脚本?

How do I run a local, non-web Ruby script with ENV variables?

我有一个.env文件

SOME_VARIABLE=1
ANOTHER_VARIABLE=2

这里是 script.rb

puts ENV['SOME_VARIABLE'], ENV['ANOTHER_VARIABLE']

我已经安装了 foreman。当我运行foreman start -e .env时,没有输出。好像什么都没发生。

我做错了什么?

您是否为工头创建了 Procfile 以 运行 任务,如果没有,您必须创建 Procfile,下面是示例代码:

    my_proc:ruby script.rb
    # Then you can use the following command to execute 
    # foreman start my_proc -e .env 

并将收到以下输出。

10:10:55 my_proc.1 | started with pid 1365
10:10:55 my_proc.1 | SOME_VARIABLE
10:10:55 my_proc.1 | 1
10:10:55 my_proc.1 | 2
10:10:55 my_proc.1 | ANOTHER_VARIABLE
10:10:55 my_proc.1 | exited with code 0
10:10:55 system    | sending SIGTERM to all processes

我的脚本文件是

puts "SOME_VARIABLE", ENV['SOME_VARIABLE'], ENV['ANOTHER_VARIABLE'], "ANOTHER_VARIABLE"

希望对您有所帮助!

干杯