How does environment check work in Ruby on Rails?
In this article, we’re going to explore the following topics:
- the
Rails.env
object - the
respond_to_missing?
hook method
Rails.env.*?
If you’re familiar with the notion of environment Ruby on Rails then you probably had to deal with Rails.env.production?
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 exist?
ActiveSupport::StringInquirer
First, let’s have a look at what’s Rails.env
Here we can see that Rails.env
is an instance of the class ActiveSupport::StringInquirer
.
This class inherits from String
.
So Rails.env
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 at what happens behind the scene when I call the production?
method on an instance of the ActiveSupport::StringInquirer
.
The method_missing hook method
When production?
is called on an instance of the ActiveSupport::StringInquirer
class then the ActiveSupport::StringInquirer#method_missing
hook method is called as production?
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.
Feel free to have a look to this article if you’re not familiar with the notion method_missing hook method.
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 production?
isn’t defined on the ActiveSupport::StringInquirer
class.
So how does the magic work 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 implements this hook method can respond.
Indeed, production?
is not defined — as its behavior is delegated to method_missing
.
So what happens if we execute this statement?
Here, we’d like this statement to return true
.
That’s where respond_to_missing?
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 of the respond_to_missing?
method.
Ruby Mastery
We’re currently finalizing our first online course: Ruby Mastery.
Join the list for an exclusive release alert! 🔔
Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.
💚