[‘Array’, ‘methods’, ‘you’, ‘might’, ‘not’, ‘know’]

I recently came across a very helpful array method, repeated_permutation, the which made my life a lot easier and my code a lot easier to read. I didn’t know this existed until another apprentice pointed out that I should check out permutations. I quickly realized that there are probably many more Ruby methods that might make my code cleaner in other situations. Let’s look at a few array methods you might not know about starting with repeated_permutation.

repeated_permutation

I was trying to create an array of possible codes for a mastermind game. There are 6 color possibilities and codes can be comprised of any four of the colors combination, and repeated colors are allowed. Let’s look at the first way I solved this and the much better Ruby way to solve it.

Color possibilities = Yellow, Blue, Red, Green, Purple, and Orange

class FirstCodeListCreator
  attr_reader :list_of_codes

  def initialize
    @colors = ['Yellow', 'Blue', 'Red', 'Green', 'Purple', and 'Orange']
    create_list
  end

  def create_list
    @colors.each do |first_color|
      @colors.each do |second_color|
        @colors.each do |third_color|
          @colors.each do |fourth_color|
            @list_of_codes << [first_color, second_color, third_color, fourth_color]
          end
        end
      end
    end
  end
end

class BetterCodeListCreator
  attr_reader :list_of_codes

  def initialize
    @colors = ['Yellow', 'Blue', 'Red', 'Green', 'Purple', 'Orange']
    create_list
  end

  def create_list
    @list_of_codes = @colors.repeated_permutation(4).to_a
  end
end

combination

This is the sister to the permutation (but for this example we’re not looking at repeated_combination). When invoked with a block, yields all combinations of length n of elements from the array and then returns the array itself.

You can use this when you want to find all the possible combinations of something when you don’t care about the order. For example, you can create a list of all possible combinations of hands of 5 euchre cards.

lass HandsOfCards
  def create_list_of_hands(suits, numbers)
    deck = create_deck(suits, numbers)
    list_of_possible_hands(deck)
  end
  
  def create_deck(suits, numbers)
    cards = []
    suits.each do |suit|
      numbers.each do |number|
        cards << [suit, number]
      end
    end
    cards
  end

  def list_of_possible_hands(deck)
    deck.combination(5).to_a
  end
end

suits = ['heart', 'club', 'spade', 'diamond']
numbers = ['A', 'K', 'Q', 'J', '10', '9']

hands = HandsOfCards.new.create_list_of_hands(suits, numbers)
puts hands.size #==> 42504

sample

Select a random element from an array with sample. So easy! Using the example above…

def select_random_hand(hands)
  hands.sample
end

cycle

Calls the given block for each element n times or forever. This method seems like it could be pretty interesting. Sure we could print elements of an array multiple times. But couldn’t we also run methods on objects using polymorphism? Let’s take look…

class CycleTest
  def cycle_test
    [ObjectOne.new, ObjectTwo.new].cycle(3) {|object| object.start}
  end
end

class ObjectOne
  def start
    puts "This is the first object."
  end
end

class ObjectTwo
  def start
    puts "This is the second object."
  end
end

CycleTest.new.cycle_test

#This is the first object.
#This is the second object.
#This is the first object.
#This is the second object.
#This is the first object.
#This is the second object.

keep_if

For our last interesting Ruby array method, I chose keep_if because it seems pretty useful.

keep_if deletes every element of an array for which the given block evaluates to false.

class WordParser
  def delete_two_letter_words(sentence)
    words = sentence.split(" ")
    big_words = words.keep_if {|word| word.length > 2}
  end
end

sentence = "I love to code in ruby"
puts WordParser.new.delete_two_letter_words(sentence) #=> ["love", "code", "ruby"]

Well that wraps up this session of array methods. Go here to explore some more methods you don’t know or don’t remember.

Leave a Reply

Your email address will not be published. Required fields are marked *