Created
March 2, 2011 23:54
-
-
Save duncanbeevers/852034 to your computer and use it in GitHub Desktop.
This file contains 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
Factory.sequence :luhn do |n| | |
def luhn?(n) | |
odd = false | |
0 == n.to_s.scan(/\d/).inject(0) { |a, d| | |
i = d.to_i * (odd = !odd ? 2 : 1) | |
a + (i > 9 ? i - 9 : i) | |
} % 10 | |
end | |
@last_luhn ||= 0 | |
begin | |
@last_luhn += 1 | |
end while !luhn?(@last_luhn) | |
@last_luhn | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment