如何检测我的应用程序是否在Heroku上运行?

Sam*_*ong 4 ruby-on-rails heroku

有没有办法检测rails应用程序是否在Heroku上运行?

我想做一个像这样的方法:

def on_heroku?
...
end
Run Code Online (Sandbox Code Playgroud)

ilt*_*mpo 7

您可以设置自己的配置变量以用于检测ENV['var'].

$ heroku config:add I_AM_HEROKU=yes

def on_heroku?
  ENV['I_AM_HEROKU']
end
Run Code Online (Sandbox Code Playgroud)


小智 5

def heroku?
  ENV.any? {|x,_| x=~ /^HEROKU/ }
end
Run Code Online (Sandbox Code Playgroud)