Voodoo send
July 12th, 2008
Recently when looking through some plugin’s code, we found the following line:
self.send(:include, Foo)
This seems to be an acceptable variant of the commonly used
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.
Leave a Reply