The dynamic nature of the Ruby language makes it simply not enough to learn rails simply by looking up the relevant API documentation, because the API documentation only records the methods of static code definitions, and many of the methods in rails are dynamically generated at load or run time, and you want to understand that these methods must read the source code. This article takes a method invocation in the Config/application.rb file in Rails engineering as an example to analyze the process of method invocation.
1. Description of the problem
Assuming that the rails project is called sample, the sample::application is defined in config/application.rb and the Config method can be called, as follows:
1 Module Sample 2 class Application < rails::application3 ... 4 " #{config.root}/lib/validators " 5 ... 6 End7 End
The Config method does not exist in the API documentation, and the following is an analysis of how the Config method is found.
2. Method Lookup
In the definition of sample::application, self=sample::application, so config is the Sample::application class method. Find the ancestor chain of Sample::application, and look it up, finally found the method in Rails::railtie, as follows:
1 sample::application.ancestors2 #= = [Firstapp::application, rails::application, Rails::engine, Rails::railtie, rails::initializable, Object, PP :: Objectmixin, Activesupport::D ependencies::loadable, V8::conversion::object, Json::ext::generator:: Generatormethods::object, Kernel, Basicobject]3 4Rails::railtie.singleton_methods (false). grep/^config$/5 #= = [: Config]6 7 #You can also use Source_location to view the location of the method8 sample::application.method (: config). source_location9 #= = ["/usr/local/rvm/gems/ruby-2.1.1/gems/railties-4.1.4/lib/rails/railtie.rb", 123]
Finally, the method definition is found in Rails::railtie, as follows:
1 Module Rails2 classRailtie3 ...4 class<< Self5 ...6 Delegate:config, to:: Instance7 ...8 End9 ...Ten End OneEnd
You can see that line 6th uses metaprogramming to define the Config method, and the Config method is not generated until the file is loaded, so the method does not appear in the API documentation.
3. Definition of the method
The definition of the Config method is interesting in that it uses rails to define the extended delegate method of module, using the delegate pattern, which is the core of the delegate approach:
1 classModule2 ...3 defDelegate (*methods)4 ...5Methods.each do |method|6 ...7Method_def = [8 "def #{method_prefix}#{method} (#{definition})",9 "_ = #{to}",Ten "if!_.nil? | | | nil.respond_to? (: #{method})", One "_.#{method} (#{definition})", A "Else", - "#{exception unless Allow_nil}", - "End", the "End" -].join';' - - module_eval (method_def, file, line) + End - End +End
According to the above code, Delegate:config, to:: instance is equivalent to the following code (omitted), that is, a class method is defined in Rails::railtie:
1 class << rails::railtie2def3instance.config 4 End5 End
4. Invocation of the method
Finally, in the problem description, Sample::application calls the method, which is equivalent to executing the following code:
1 sample::application.instance.config
instance method is a class method, defined in Rails::railtie, The definitions and the intermediate results obtained from the instance code are as follows:
1 # 2 class << self 3 def instance 4 @instance | | = new 5 End 6 end 7 # According to instance definition, get intermediate result 9 Sample:: Application.new.config
At this point self is the Sample::application.new,config method is an instance method, defined in Rails::application, whose definition and final execution results are as follows:
1 #definition of the Config method2 defConfig#: Nodoc:3@config | | = Application::configuration.new (Find_root_with_flag ("config.ru", dir.pwd))4 End5 6 #Final execution Results7Application::configuration.new (Find_root_with_flag ("config.ru", dir.pwd))
This allows you to know how the Config method is called.
5. Summary
The above analysis of an example of a method call in Rails::application, it can be seen is still relatively complex, especially in the judgment of self to be absolutely clear, otherwise it may be called method to produce a judgment error. The Module#delegate method is a commonly used delegate method in rails and will be discussed in detail in the future.