Skip to content

Instantly share code, notes, and snippets.

@metalelf0
Created November 16, 2010 20:20
Show Gist options
  • Save metalelf0/702437 to your computer and use it in GitHub Desktop.
Save metalelf0/702437 to your computer and use it in GitHub Desktop.
Mixin VS Monkey patching
######### 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]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment