Yes, before_filter
is a method on ActionController::Base. Anything specified in before_filter
will 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:
>> class Hello
>> p "hello"
>> end
"hello"
so in the case you mentioned ruby sees the before_filter
method 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 to Object
. In this case, it will go up to the ActionController::Base class and look for the before_filter
and then up the chain to class, module and object.
>> ActionController::Base.class
=> Class
>> ActionController::Base.class.superclass
=> Module
>> ActionController::Base.class.superclass.superclass
=> Object
>> ActionController::Base.class.superclass.superclass.superclass
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.