Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4616

Implementing the Luhn validation algorithm in Ruby

$
0
0

I've been trying to implement the Luhn algorithm in Ruby. I've been following these steps:

  • The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:
    • Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
    • Sum the digits of the products (e.g., 10 = 1 + 0 = 1, 14 = 1 + 4 = 5) together with the undoubled digits from the original number.
    • If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

http://en.wikipedia.org/wiki/Luhn_algorithm

This is what I've came up with:

 def validCreditCard(cardNumber)    sum = 0    nums = cardNumber.to_s.split("")    nums.insert(nums.size, "x")    nums.reverse!    nums.each_with_index do |n, i|        if !n.eql?("x")            sum += (i % 2 == 0) ? n.to_i : n.to_i * 2        end    end    if (sum % 10) == 0        return true    else        return false    endend

However, this returns false every time I test it. I am not sure what I am doing wrong.


Viewing all articles
Browse latest Browse all 4616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>