Les Hill github twitter facebook linked in archives
Posted July 12, 2008

Recently when looking through some plugin’s code, we found the following line:

1 self.send(:include, Foo)

This seems to be an acceptable variant of the commonly used

1 ActiveRecord::Base.send(:include, Foo)

As you may have surmised, it is not an acceptable variant. This is a Voodoo programming smell, as it indicates a misunderstanding of exactly what the semantics of send are. There is also a clearer, more intention revealing, and identical1 alternative include Foo or with the omitted parenthesis include(Foo) .

This of course, leads us to why ActiveRecord::Base.send(:include, Foo) is acceptable. The include method is private, so the most elegant way to invoke it on another object is by using send; this is possible because send ignores access control allowing us to invoke include directly.

1 Almost, the difference is that send is in the call stack.

Thanks to Tom Preston-Werner for the CSS layout, Webby for the blog renderer, and GitHub Pages for the blog hosting.