in ruby you can have:
class ApplicationController < ActionController::Base
before_filter :require_login
end
i just wonder what is before_filter? it’s a method from ActionController::Base?
and what will happen if i create an object of ApplicationController? The before_filter method will be run?
thanks!
Yes,
before_filteris a method on ActionController::Base. Anything specified inbefore_filterwill run before the action(s) being called.API Documentation: http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000316
EDIT:
When you write directly into a class, that code is executed when the class is loaded into the interpreter.
Typing this into IRB:
so in the case you mentioned ruby sees the
before_filtermethod and it tries to find it. It starts looking in its class, then it goes into the parent and the parent’s parent, and so on until it gets toObject. In this case, it will go up to the ActionController::Base class and look for thebefore_filterand then up the chain to class, module and object.If you are up for reading, I highly recommend MetaProgramming Ruby, it does a much better job of explaining the object model than I can.