Ruby Mixin and monkey patching examples

Let’s explore a couple of solutions to dynamically add a split_by_half behaviour to an array object. The first technique is the mixin: it allows to add the method to a single array instance. The second one is called monkey patching, and adds the method directly to the Array class, adding this behaviour to every array instance. ######### MIXIN module SplittableArray def split_by_half middle = (self.size.to_f / 2).floor return [self[0....

April 23, 2010 · 1 min · metalelf0