Skip to content

Instantly share code, notes, and snippets.

@thomasfedb
Created December 16, 2010 02:32
Show Gist options
  • Save thomasfedb/742932 to your computer and use it in GitHub Desktop.
Save thomasfedb/742932 to your computer and use it in GitHub Desktop.
def luhn_valid?(value)
return false unless value.is_a?(String) && !value.strip.empty?
value.gsub!(/\D/, '')
sum = 0
value.split(//).each_with_index do |number, index|
if index.even?
sum += (number * 2).to_s.split(//).map(&:to_i).sum
else
sum += number
end
end
sum % 10 == 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment