Last active
September 24, 2015 18:18
-
-
Save vvalgis/789593 to your computer and use it in GitHub Desktop.
Credit card number validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ccn_valid?(number) | |
(number.to_s.split(//).inject([]){ |r,d| r << (r.count.even? ? (d.to_i * 2).to_s : d); r }.map{|d| d.split(//)}.flatten.inject(0){|sum,d| sum += d.to_i} % 10) == 0 | |
end | |
puts ccn_valid?("4417123456789113") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment