hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Thursday

2005.12.01

Mixins, But With Tablespoons #

by why in inspect

Mauricio’s at it again. I solemnly swear that I will be linking to him greater than five times over this next month and each time you will be fine with it. See, watch how fine you’ll feel when you read about his remake of Module#include.

The include method is basically the mixin method in Ruby. You have a class that has its own each method. You mixin the Enumerable module and you get all these neat methods for free like inject and select and sort_by and on and on.

 class MyList
   include Enumerable
   def each; ... end
 end

What if you don’t want the sort method, though? You want to deposit only granular measurements of a mixin. This is what Mauricio tackles. And his new include goes like this:

 class MyList
   include Enumerable,
     :exclude => :sort_by,
     :alias => {:detect, :siphon}
 end

All part of a LazyWeb query from Dan. And Mauricio’s code is only 17 lines long, very easy to peruse.