... mostly about Ruby and Rails...

Mittwoch, 20. Juni 2007

Quickie: using alias_method_chain

... while doing some development on my ddcplugin, I had to use the feature alias_method_chain to extend some of ActionController's functionality, but I wasn't able to find a good description on the net on how to use that. So, here is a quicky about that.

You have:

class MyClass
def aMethod
puts "Hi"
end
end

you want to add a feature to aMethod

class MyClass
def aMethod_with_feature
puts "feature"
aMethod_without_feature
end
alias_method_chain :aMethod, :feature
end

Ok, simply put, you have to name the new method 'aMethod_with_feature'. If you want to call the old method, you use 'aMethod_without_feature'. The rest is done by alias_method_chain and your new functionality will be in. Remember that you have to specify the names of the methods as symbols. *thumbs_up*

-alex


References:

“alias_method_bling
New in Rails: Module#alias_method_chain
Interesting technique using #alias_method_chain

Keine Kommentare: