Ruby-on-rails Rails.env 与 RAILS_ENV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2715035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails.env vs RAILS_ENV
提问by brad
I see both in examples when checking what env one is running in. What's preferred? Are they, for all intents and purposes equal?
在检查正在运行的环境时,我在示例中看到了两者。什么是首选?就所有意图和目的而言,它们是否相等?
回答by Mark Rushakoff
According to the docs, #Rails.envwraps RAILS_ENV:
根据文档,#Rails.env包装RAILS_ENV:
# File vendor/rails/railties/lib/initializer.rb, line 55
def env
@_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
end
But, look at specifically howit's wrapped, using ActiveSupport::StringInquirer:
但是,具体看看它是如何包装的,使用ActiveSupport::StringInquirer:
Wrapping a string in this class gives you a prettier way to test for equality. The value returned by Rails.env is wrapped in a StringInquirer object so instead of calling this:
Rails.env == "production"you can call this:
Rails.env.production?
在此类中包装字符串为您提供了一种更漂亮的方法来测试相等性。Rails.env 返回的值被包装在一个 StringInquirer 对象中,而不是这样调用:
Rails.env == "production"你可以这样称呼:
Rails.env.production?
So they aren't exactlyequivalent, but they're fairly close. I haven't used Rails much yet, but I'd say #Rails.envis certainly the more visually attractive option due to using StringInquirer.
所以它们并不完全相同,但它们相当接近。我还没有经常使用 Rails,但我认为#Rails.env由于使用StringInquirer.
回答by superluminary
ENV['RAILS_ENV']is now deprecated.
ENV['RAILS_ENV']现在已弃用。
You should use Rails.envwhich is clearly much nicer.
您应该使用Rails.env它显然更好。
回答by Simone Carletti
Before Rails 2.x the preferred way to get the current environment was using the RAILS_ENVconstant. Likewise, you can use RAILS_DEFAULT_LOGGERto get the current logger or RAILS_ROOTto get the path to the root folder.
在 Rails 2.x 之前,获取当前环境的首选方法是使用RAILS_ENV常量。同样,您可以使用RAILS_DEFAULT_LOGGER获取当前记录器或RAILS_ROOT获取根文件夹的路径。
Starting from Rails 2.x, Rails introduced the Railsmodule with some special methods:
从 Rails 2.x 开始,Rails 引入了Rails一些特殊方法的模块:
- Rails.root
- Rails.env
- Rails.logger
- Rails.root
- Rails.env
- Rails.logger
This isn't just a cosmetic change. The Rails module offers capabilities not available using the standard constants such as StringInquirersupport.
There are also some slight differences. Rails.rootdoesn't return a simple Stringbuth a Pathinstance.
这不仅仅是表面上的改变。Rails 模块提供了使用标准常量(例如StringInquirersupport)无法提供的功能。也有一些细微的差别。Rails.root不返回一个简单String但Path实例。
Anyway, the preferred way is using the Railsmodule. Constants are deprecated in Rails 3 and will be removed in a future release, perhaps Rails 3.1.
无论如何,首选方法是使用Rails模块。常量在 Rails 3 中已被弃用,并将在未来的版本中删除,也许是 Rails 3.1。
回答by JuYo
Strange behaviour while debugging my app: require "active_support/notifications" (rdb:1) p ENV['RAILS_ENV'] "test" (rdb:1) p Rails.env "development"
调试我的应用程序时的奇怪行为:需要“active_support/notifications”(rdb:1) p ENV['RAILS_ENV'] “test” (rdb:1) p Rails.env “development”
I would say that you should stick to one or another (and preferably Rails.env)
我会说你应该坚持一个或另一个(最好是 Rails.env)
回答by jgpawletko
Update: in Rails 3.0.9: env method defined in railties/lib/rails.rb
更新:在 Rails 3.0.9 中:railties/lib/rails.rb 中定义的 env 方法

