Take the 2-minute tour ×

Rails has these cool properties that seem to be actually methods. For example:

class SomeController < ApplicationController

  before_filter :authenticate!


What are these actually called and how would you create your own? For example, in one of my models I want to be able to have a dynamic property that selects an internal method for processing some results:

class MyModel < ActiveRecord::Base

    active_method :some_class_method


How would I set this up so I can set active_method like that and be able to access the active_method symbol as an instance var?

Edit for elaboration:

So give this starter below, I need to figure out how to define "selected_method" so that it defines a accessor or instance variable so "called_selected_method" calls "method_b".

class MyClass

  selected_method :method_b

  def call_selected_method



  def method_a
    puts 'method_a'

  def method_b
    puts 'method_b'


c = MyClass.new
c.call_selected_method # should put 'method_b'
share|improve this question
Can you elaborate? What exactly is the property you say is "actually a method"? Are you talking about before_filter? –  Ray Toal Sep 11 '11 at 5:15
Just like Chris said, the idea is that before_filter is a class method available to SomeController through inheritance. When before_filter is called (line 3 in your code), it maintains the list of arguments passed to it in a class variable, so that it can be used later by other methods of that class. These methods can be instance methods or class methods. It goes without saying that the 'maintains the list of arguments' part is done in a way that makes design sense. –  Swanand Sep 11 '11 at 8:43
add comment

1 Answer

up vote 3 down vote accepted

It's actually just a method call to a method defined on the class. before_filter is provided by a ruby Module, which is mixed in to ActionController.

Creating your own methods similar to before_filter is as easy as:

  1. Define a class method on your Class
  2. Call that method in any concrete implementations of your class.

Some example code:

class MyClass
  class << self
    def some_function(*args)
      # your code here

  some_function "foo"

If you wanted to abstract it further, you can put the class method in to a Module, and then include that module in to your class(es).


In relation to your asking of how to get a call of some_function to set an instance variable on your class, you can't, as class methods cannot affect specific instances of that class.

I have to wonder, though... you're writing a method that will just act as a proxy to your other method, and would be hard-coded in to the class definition. That offers no benefit to you, and would just make your code redundantly complicated.

share|improve this answer
in rails 3 it's provided by AbstractController::Callbacks. In both cases, the base functionality comes from ActiveSupport::Callbacks. –  numbers1311407 Sep 11 '11 at 5:16
Indeed you are correct. As it's not particularly important for the answer, I've removed that part. –  Chris Sep 11 '11 at 5:20
+1. Specifically for the last paragraph of the answer. –  Swanand Sep 11 '11 at 8:42
You're right, it does offer very little benefit. I was trying to show a simple example of what I was trying to achieve. What I learned is that instance variables won't work. Thanks! –  typeoneerror Sep 11 '11 at 14:40
add comment

Your Answer


