我如何找到 ruby irb 上的 ENV['RACK_ENV']?

How do I find out the ENV['RACK_ENV'] on the ruby irb?

我的 gem 文件中有以下行。

if defined? ENV['RACK_ENV'] == 'production' then gem 'pg' else gem 'sqlite3' end

Heroku it uses the pg gem as it sees it's in production and on my computer it uses the sqlite gem. However on Openshift 上,它说找不到 pg gem 所以它不能 运行 应用程序,所以显然 ENV['RACK_ENV'] 不认为它正在生产中,也许正在开发中。

如何在 irb、rack 或 ruby 命令行上找到 ENV['RACK_ENV'] 的变量?

在 Heroku 上,已经为您设置了 RACK_ENV 环境变量,在您的本地计算机(或者显然是 Openshift)上不会出现这种情况。在 Ruby 读取未设置的环境变量 returns nil.

$ export ANY_ENV=foo
$ irb
irb(main):001:0> puts ENV['ANY_ENV']
foo
=> nil
irb(main):002:0> puts ENV['ANOTHER_ENV']

=> nil
irb(main):003:0> ENV['ANOTHER_ENV'].nil?
=> true

在 Openshift 上,您需要通过 rhc env set RACK_ENV=production -a YOUR_APP_NAME

RACK_ENV 设置为 production

他们提供了三种查看环境变量的方式:

  • 将导出语句添加到 App_Name/.openshift/action_hooks/build 文件,然后 运行 git push。变量列在 Git 输出中并以 remote: declare -x.
  • 开头
  • 在 shell 提示符下使用 SSH 和 运行 env 命令访问应用程序。
  • 使用rhc env list -a <appname> command

有关在 Openshift 上设置环境变量的更多详细信息,请参阅 https://developers.openshift.com/en/managing-environment-variables.html