This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Mixins, But With Tablespoons
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
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.