Thursday, 22 March 2007

Named Callbacks

Coming from a Java background I often find myself wanting to override constructors and other hooks. I should really pay more attention to the documentation.

Instead of:
class MyClass
def after_initialize
#Do Something...
end
end

Do:

class MyClass
after_initialize :do_something

def do_something
#Do Something...
end
end

Update 13/06/07:

Using named callbacks better conveys your intention, making your code more readable.
Similar article on The Rails Way

No comments: