Rails.env 与 RAILS_ENV [英] Rails.env vs RAILS_ENV

查看:43
本文介绍了Rails.env 与 RAILS_ENV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检查正在运行的环境时,我在示例中看到了两者.首选什么?就所有意图和目的而言,它们是否相同?

I see both in examples when checking what env one is running in. What's preferred? Are they, for all intents and purposes equal?

推荐答案

根据文档#Rails.env 包装了 RAILS_ENV:

    # File vendor/rails/railties/lib/initializer.rb, line 55
     def env
       @_env ||= ActiveSupport::StringInquirer.new(RAILS_ENV)
     end

但是,使用 ActiveSupport::StringInquirer 来具体查看如何包装它:

But, look at specifically how it's wrapped, using ActiveSupport::StringInquirer:

在这个类中包装一个字符串给出你是一种更漂亮的测试方式平等.返回的值Rails.env 被包裹在一个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"

你可以这样称呼:

Rails.env.production?

所以它们并不完全等价,但它们相当接近.我还没有经常使用 Rails,但我想说 #Rails.env 由于使用了 StringInquirer,所以在视觉上更有吸引力.

So they aren't exactly equivalent, but they're fairly close. I haven't used Rails much yet, but I'd say #Rails.env is certainly the more visually attractive option due to using StringInquirer.

这篇关于Rails.env 与 RAILS_ENV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆