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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######### MIXIN | |
module SplittableArray | |
def split_by_half | |
middle = (self.size.to_f / 2).floor | |
return [self[0..middle], self[middle+1..self.size]] | |
end | |
end | |
some_array = [1, 2, 3, 4, 5] | |
some_array.extend SplittableArray | |
some_array.split_by_half # => [[1, 2, 3], [4, 5]] | |
######### MONKEY PATCH ARRAY | |
class Array | |
def split_by_half | |
middle = (self.size.to_f / 2).floor | |
return [self[0..middle], self[middle+1..self.size]] | |
end | |
end | |
another_array = [6, 7, 8, 9, 10] | |
another_array.split_by_half # => [[6, 7, 8], [9, 10]] |