Rails.env 与 RAILS

编程入门 行业动态 更新时间:2024-10-25 04:21:22
本文介绍了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

本文发布于:2023-10-28 13:26:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   env   RAILS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!