How to split arrays

Notes on the Le Wagon Iteratros & Blocks Splitter exercise

We want to create a splitter method which will take an array and divide it into two groups according to an arbitrary rule. You might want to split by age if we’re talking about a group of people.

Lets implement a method size_splitter which takes two parameters: an array, and a integer (the size). We will assume that the array only contains words, e.g. Strings, and that the arbitrary rule is to form two groups: the first one with words of the given size (second parameter of the method), and the other group with all the other words.

The size_splitter method should return an array of two arrays - the two groups defined above - with the contents sorted alphabetically.

  def size_splitter(array, size)
  size_array = do |word|
    word.length == size
  other_array = array.reject do |word|
    word.length == size
  combined_array = []
  combined_array.push(size_array.sort, other_array.sort)
  return combined_array

We start by calling the method .select on to the array. The block then check to see if each word in the array is of the length size and if it is it is added to the size_array.

The other array uses to .reject to add words not equal to the length of size to the array. It bascially rejects words that are equal to size.

We then set the combined_array to empty and use the ruby method .push to add in each array along with the .sort method which orders them alphabetically.

In the previous exercise, the abritrary rule was fixed. What if we wanted to let the method caller choose which rule to apply? We can do so with the power of blocks and yield.

def block_splitter(array)
  true_array = do |word|
    yield(word) == true && word.start_with?("a")
  other_array = array.reject do |word|
    yield(word) == true
  new_array = []
  new_array.push(true_array, other_array)
  return new_array

This is the same concept although here we use yield with the parameter of word which allows the user to customzie the rule. This is then checked against whether it is equal to true and also equal to the word starting with an “a”. The second array then rejects anything which is not true.

The new_array is created and set to empty and we use .push to both the true_array and the other_array into the new_array.

We then return the new_array.