Skip to content

Instantly share code, notes, and snippets.

@Sutto
Forked from thomasfedb/luhn.rb
Created December 16, 2010 03:10
Show Gist options
  • Select an option

  • Save Sutto/742975 to your computer and use it in GitHub Desktop.

Select an option

Save Sutto/742975 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