Wednesday, January 23, 2008

Mixins

In Ruby, there's something called mixins. What is this? Basically, they are mix of a class and modules. Modules can be thought of as relatively independent pieces of code that you may want to mix into your classes. Modules are used to keep the code modular and easier to maintain.

For example, if we wanted to have the ability to compare objects, we could create a Comparable module. This module would define the functions in order to compare objects. Some objects do not require this function however, so we do not need to add it to a base class. Instead, we can include this module to only those classes that require it. This is, of course, overly simplified, but it is only to illustrate the purposes of modules.

For a quick tutorial on mixins, visit this site:

http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/


W

No comments: