How environment check works in Ruby on Rails?

Mehdi Farsi
Jul 17 · 3 min read

In this article, we’re going to explore the following topics:

  • the Rails.env object
  • the respond_to_missing? hook method


If you’re familiar with the notion of environment in the Ruby on Rails framework then you probably had to deal with the Rails.env.production? method in order to control an execution flow depending on the current environment

rails> Rails.env.production?
=> false
rails> Rails.env.development?
=> true
rails> Rails.env.test?
=> false

In the above example, we can see that Ruby on Rails provides a method per environment.

But what if I tell you that these methods don’t really exist?


First, let’s have a look to what’s Rails.env

Here we can see that the Rails.env object is an instance of the class ActiveSupport::StringInquirer.

This class inherits from String.

So the Rails.env object is a string that represents the current environment.

Now that we’re more familiar with the Rails.env attribute, then let’s have a look to what happens behind the scene when I call the production? method on an instance of the ActiveSupport::StringInquirer.

The method_missing hook method

When the production? method is called on an instance of the ActiveSupport::StringInquirer class then the ActiveSupport::StringInquirer#method_missing hook method is called as the production? methods isn’t implemented in this class.

This hook method is invoked when the ActiveSupport::StringInquirer method lookup path is traversed and none of the classes can handle the production? message.

This hook method behaves as followed:

1- it checks if the unhandled message contains a ? suffix.

2- if so, it compares self with the message name without the ? suffix.

3- finally, the result of this comparison is returned.

The respond_to? production? returns true even though the production? method isn’t defined on the ActiveSupport::StringInquirer class.

So how the magic works behind the scene?

The respond_to_missing? hook method

When you implement a strategy using the method_missing hook method, then it’s difficult to know to which method the class that implement this hook method can respond.

Indeed, our production? method is not defined — as its behavior is delegated to the method_missing.

So what happens if we execute this statement?

Here, we’d like that this statement returns true.

That’s where the respond_to_missing? method comes into play

Here, we define a respond_to_missing? hook method that is implicitly called when we invoke respond_to? on an instance of the ActiveSupport::StringInquirer class.

This method returns a boolean that is true if the last character of the method_name argument is a ? or it calls super otherwise — which returns false by default.

So, in our case, Rails.env.respond_to :production? returns true because it matches the condition in the respond_to_missing? method.



Feel free to subscribe here:

Thank you for taking the time to read this post :-)

Feel free to 👏 and share this article if it has been useful for you. 🚀

Also, as I post a bunch of articles per month.

So feel free to follow me to get a notification when I release an article.

Here is a link to my last article: Object equality in Ruby


E-Learning platform for Ruby and Ruby on Rails

Mehdi Farsi

Written by

Founder of — I blog about Ruby & ROR - Paris


E-Learning platform for Ruby and Ruby on Rails

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade