Using Mixins With Rails

Posted by Tres Thu, 28 Aug 2008 12:53:00 GMT

Mixins are one of the most powerful constructs that Ruby provides. Mixins give you the power of multiple inheritance without all the inherent complexities and problems associated with multiple inheritance. The best part of using mixins is how extremely simple they are to use. Just include the module you want to mixin and then call any of the functions defined in the module.

Using mixins with rails is just as easy, the only problem is figuring out where to put your mixin (opinionated programming, you know). Just put your mixin into the lib directory of your rails project and you’ll be good to go. Once you’ve got the file in the right spot, just

  require 'module_file_name'
  include ModuleName

Then call the function you want to use, and you’re set.

Posted in , ,  | Tags ,

Comments are disabled