Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Created March 2, 2011 23:54
Show Gist options
  • Save duncanbeevers/852034 to your computer and use it in GitHub Desktop.
Save duncanbeevers/852034 to your computer and use it in GitHub Desktop.
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