Currently under development!

Rails: Environment Configuration (Magical) Instance

13 April 2024

2 mins

Rails: Environment Configuration (Magical) Instance

After instantiating your rails application, we have codes generated for us. Let’s focus on a particular section. Focus on the environments section. That is the config/environments folder. This folder will contain three files to set up rails behavior in different environments.

Looking at any of these files will bring us to the question we want answers to in this article.

The following is a sample of the file (without the comments).

Rails.application.configure do

  config.cache_classes = true

  config.eager_load = true

end

The Question

If you look at the code, you will notice something. config is being used everywhere. But the question is, Where is config coming from? Where is config defined?

It seems magical. An ideal scenario would be that config was passed as a parameter. Like this.

Rails.application.configure do |config|

  config.cache_classes = true

  config.eager_load = true

end

So why does the original code work without the parameter? Let’s dig a little.

Unveiling the Trick

First, we need to understand that the Rails module object has an application instance object which is created when we inherit from Rails::Application. This is not in the scope of this article. Trying to explain the first line, why Rails.application.

The application instance has a method “configure”. This method is an inherited method from “Rails::Railtie”. You can find the code here.

def configure(&block) # :nodoc:
    instance_eval(&block)
end

As you can see from the above code, the method accepts a block as an argument. So the block from the first code block of this article is passed to this argument.

Now we have gotten to the heart of the trick. And it is instance_eval.

instance_eval is a method that accepts block or string. passing the block to it executes the content of the block as though it was defined in the object.

When the block is running and trying to run config, it checks in the application object context and uses it. We can see that config was defined in the application instance here.

def config
    @config ||= Railtie::Configuration.new
end

In other words, instance_eval gives us a portal into the application instance object. So we can use other methods and variables inside the application instance object in the block not just config.

Conclusion

I have shown that the reason for this confusing part was because of the instance_eval giving us portal access to the application instance. Hope I helped better understand this Rails-generated code and give you confidence in editing it and creating better programs.

As always thanks for your time and happy coding.