0
module YourApp
   class Application < Rails::Application
      config.my_custom_variable = :custom_value
   end
end

This works in my Rails application. I just want to understand how this works from ruby perspective. As per to my minimal ruby knowledge, there must be getter and setter(my_custom_variable=) for my_custom_variable in the config(Rails::Application::Configuration) object. Since this is my custom variable this will not be present in the Configuration object instance. How is it dynamically created/added. ?

Can somebody please explain?, direct me to the proper documentation to understand this.

2 Answers 2

2

Rails is using method_missing here to catch any method called on config. Then, it just adds it into a hash of options.

You can see the relevant source code here.

Sign up to request clarification or add additional context in comments.

Comments

0

Not how Rails implements it, but achieve similar functionality in Ruby

require 'ostruct'

module YourApp
    class Application
        @@config = OpenStruct.new

        def self.config
           return @@config
        end
    end
end

YourApp::Application.config.my_custom_variable = :custom_value
puts YourApp::Application.config.my_custom_variable
>> custom_value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.