dennismonsewicz
dennismonsewicz

Reputation: 25542

Nil can't be coerced into Fixnum

I have the following function:

def valid_credit_card?(number)
    digits = number.scan(/./).map(&:to_i)
    check = digits.pop

    sum = digits.reverse.each_slice(2).map do |x, y|
        [(x * 2).divmod(10), y]
    end.flatten.inject(:+)

    (10 - sum % 10) == check
end

But for some reason I keep getting the following error message: nil can't be coerced into Fixnum

And for some reason I can't figure out why the error is being thrown. Any ideas why this might be happening?

Upvotes: 0

Views: 2034

Answers (1)

pje
pje

Reputation: 22697

Your method fails when digits has an odd number of elements. In that case, when you call each_slice(2) on the last iteration, x will be the last element of digits and y will be nil. So when you get to the inject(:+) phase, the last element of the array is nil, and your coercion error happens when the interpreter hits something like 2 + nil.

Why not add an initial check on the number of digits inputted? Something like:

return false unless digits.length == 16

Upvotes: 2

Related Questions