Always Pass a Block When Using alias_method_chain

Posted by Larry Karnowski Fri, 29 Aug 2008 15:00:00 GMT

If you're using Rails' alias_method_chain mechanism, please make sure that your new method always takes a block and passes that block up the chain, even if you don't need the block yourself. Consider this being a good "alias_method_chain" neighbor. Here's why -- if you don't, no one who comes along behind you can add a block to the chain. You've effectively "block-blocked" them.

Here's an example. I wanted to add some functionality to Rails' error_messages_for method. Specifically, I wanted to be able to pass a block to it that implemented a tiny DSL for massaging the errors output before passing them up the chain to the normal error_messages_for functionality. I implemented this by adding an alias_method_chain layer on top of the error_messages_for.

In my tests this worked fine, but when I put it in my actual Rails project, the method just stopped working. It took me a while to find that another developer on the project was also alias_method_chaining the error_messages_for, but was not taking a block, and thus not passing it up the chain. In effect, he was dropping my block on the floor. No good.

So here's the template you should follow when creating an alias_method_chain:

def method_with_something_cool(arg1, arg2, &block)
  ... your custom pre-chain code ...

  method_without_something_cool(arg1, arg2, &block)

  ... your custom post-chain code ...
end

alias_method_chain :method, :something_cool

Call the old method with a block even if the old method never took a block! Don't be a cock-blocker! Err.... block-blocker!

Tags , , ,  | 16 comments

Comments

  1. Dan Manges said 3 days later:

    I'd only do this if the method that you're aliasing takes a block. If you do it all the time, you're adding something to the method that probably won't be used, making somebody else wonder why it's there. If you're looking to add a block argument to an existing method that doesn't take a block, I'd consider another approach.

  2. ebook search said 10 months later:

    It is all that damn difficult 8(

  3. auto insurance said about 1 year later:

    It is all that damn difficult 8(

  4. ucvhost said about 1 year later:

    thanks for the great post

  5. ucvhost said about 1 year later:

    great post

  6. ucvhost said about 1 year later:

    nice article really iam enjoying

  7. Telecommunication degree said about 1 year later:

    If you're looking to add a block argument to an existing method that doesn't take a block, I'd consider another approach.

  8. Web Development degree said about 1 year later:

    I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

  9. Criminal Justice degree said about 1 year later:

    I'm the same way, I do my best to remain neutral. It's hard, if you communicate with the person the other person dislikes, then you fall out of favor with them! I simple can't dislike a person, just because someone else does, I just can't.

  10. Online Policing degree said about 1 year later:

    The difference between the right word and the almost right word is really a large matter — it's the difference between a lightning bug and the lightning.

  11. health science school said about 1 year later:

    Thanks for sharing. i really appreciate it that you shared with us such a informative post..

  12. Watch Series said about 1 year later:

    A miniseries in a serial storytelling medium is a production which tells a story in a planned limited number of episodes.

  13. submit free articles said about 1 year later:

    Otto, yeah, rebase is pretty much the same for git-svn or plain git. The things I mentioned above apply in both cases.

  14. frwc's royal trader said about 1 year later:

    Trade flows are an important factor in the long-term direction of a currency's exchange rate.

  15. watches for sale said about 1 year later:

    Otto, yeah, rebase is pretty much the same for git-svn or plain git.

  16. cheap auto insurance quotes said about 1 year later:

    If you're looking to add a block argument to an existing method that doesn't take a block, I'd consider another approach.

(leave url/email »)

   Comment Markup Help Preview comment