Skip to content

Instantly share code, notes, and snippets.

@brittanmcg
Created December 13, 2013 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brittanmcg/7939106 to your computer and use it in GitHub Desktop.
Save brittanmcg/7939106 to your computer and use it in GitHub Desktop.
Not quite sure why this will not raise an error when the number passed in is > or < 16.
class CreditCard
def initialize(nums)
@nums = nums
end
def check_card
raise ArgumentError unless @nums.length == 16
s1 = s2 = 0
@nums.to_s.reverse.chars.each_slice(2) do |odd, even|
s1 += odd.to_i
double = even.to_i * 2
double -= 9 if double >= 10
s2 += double
end
(s1 + s2) % 10 == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment