Rails.env vs RAILS_ENV

前端 未结 5 1016
难免孤独
难免孤独 2020-11-30 17:38

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

5条回答
  •  情书的邮戳
    2020-11-30 18:28

    According to the docs, #Rails.env wraps RAILS_ENV:

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

    But, look at specifically how it's wrapped, using 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?
    

    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.

提交回复
热议问题