Stuck on an algorithm. Array cant be coerced

Im stuck on this. The challenge was to take a number and multiply it by its own digits and push that onto the array until you get two adjacent numbers in the array. I keep getting an Array cant be coerced into integer on the 2nd level of the block at line 10. No idea how digits is switching from an array to integer X amount of loops through the block.

If anyone could help me out id appreciate it a ton.

require 'byebug'
def ArrayChallenge(num)
  # code goes here
  return false if num.get_digits.adjacent_pair?

  check = [pair(num.get_digits, 1)]
  loop do
    digits, multiplications = check.shift
    digits.get_digits.each do |digit|
      multi_digits = (digit * digits).get_digits
      return multiplications if digits.last == multi_digits.first || multi_digits.adjacent_pair?

      check.push(pair(multi_digits, multiplications + 1))

def pair(a, b)
  a.hash < b.hash ? [a, b] : [b, a]

class Object
  def get_digits

  def get_num

  def adjacent_pair?
    digits = self
    digits.each_with_index do |digit, index|
      return true if digit == digits[index + 1]

puts ArrayChallenge(8)


➜  ~ ruby rubytest.rb
Traceback (most recent call last):
        6: from rubytest.rb:40:in `<main>'
        5: from rubytest.rb:7:in `ArrayChallenge'
        4: from rubytest.rb:7:in `loop'
        3: from rubytest.rb:9:in `block in ArrayChallenge'
        2: from rubytest.rb:9:in `each'
        1: from rubytest.rb:10:in `block (2 levels) in ArrayChallenge'
rubytest.rb:10:in `*': Array can't be coerced into Integer (TypeError)

If I put a puts before line 10 of digits.class I get this output:

➜  ~ ruby rubytest.rb
Traceback (most recent call last):
        5: from rubytest.rb:41:in `<main>'
        4: from rubytest.rb:7:in `ArrayChallenge'
        3: from rubytest.rb:7:in `loop'
        2: from rubytest.rb:9:in `block in ArrayChallenge'
        1: from rubytest.rb:9:in `each'
rubytest.rb:12:in `block (2 levels) in ArrayChallenge': undefined method `last' for 1:Integer (NoMethodError)

Not sure what the challenge is here. Could you elaborate or post the site where the challenge is posted?

Can you share the link or document where you have the challenge?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs