I am writing an implementation of a binary search tree and in doing this I need a method that splits an array in two.
I am unsure where it is appropriate to place this method. What I mean by "where to place" is if I am going to monkey patch the array class, create a class method belonging to Tree, create a global method or lastly do something else which I have not thought of.
Here is some code to further describe the alternatives I have thought of.
Alternative 1: Monkey patch
class Array
def split
each_slice(@size/2).to_a
end
end
Alternative 2: Class method
class Tree
def Tree.split(a)
a.each_slice(a.size/2).to_a
end
end
Alternative 3: Global method
def Tree.split(a)
a.each_slice(a.size/2).to_a
end
Where is it appropriate to place this method?
As always thanks in advance for answering my question
Olav